2906. Construct Product Matrix Medium
1/**
2 * [2906] Construct Product Matrix
3 *
4 * Given a 0-indexed 2D integer matrix <font face="monospace">grid</font><font face="monospace"> </font>of size n * m, we define a 0-indexed 2D matrix p of size n * m as the product matrix of grid if the following condition is met:
5 *
6 * Each element p[i][j] is calculated as the product of all elements in grid except for the element grid[i][j]. This product is then taken modulo <font face="monospace">12345</font>.
7 *
8 * Return the product matrix of <font face="monospace">grid</font>.
9 *
10 * <strong class="example">Example 1:
11 *
12 * Input: grid = [[1,2],[3,4]]
13 * Output: [[24,12],[8,6]]
14 * Explanation: p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24
15 * p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12
16 * p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8
17 * p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6
18 * So the answer is [[24,12],[8,6]].
19 * <strong class="example">Example 2:
20 *
21 * Input: grid = [[12345],[2],[1]]
22 * Output: [[2],[0],[0]]
23 * Explanation: p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2.
24 * p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0.
25 * p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0.
26 * So the answer is [[2],[0],[0]].
27 *
28 * Constraints:
29 *
30 * 1 <= n == grid.length <= 10^5
31 * 1 <= m == grid[i].length <= 10^5
32 * 2 <= n * m <= 10^5
33 * 1 <= grid[i][j] <= 10^9
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/construct-product-matrix/
39// discuss: https://leetcode.com/problems/construct-product-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn construct_product_matrix(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
45 vec![]
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_2906() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.