Given a 0-indexed 2D integer matrix grid 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:
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 12345.
To compute the product of all elements except grid[i][j] for each cell, we can flatten the matrix, compute prefix and suffix products, and then reconstruct the result. This avoids division and works for any grid shape.
classSolution {
public: vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
vector<int> flat;
for (auto& row : grid) for (int x : row) flat.push_back(x);
int sz = n * m;
vector<int> pre(sz+1, 1), suf(sz+1, 1);
for (int i =0; i < sz; ++i) pre[i+1] = pre[i] * flat[i] %12345;
for (int i = sz-1; i >=0; --i) suf[i] = suf[i+1] * flat[i] %12345;
vector<vector<int>> ans(n, vector<int>(m));
for (int i =0; i < sz; ++i) ans[i/m][i%m] = pre[i] * suf[i+1] %12345;
return ans;
}
};
classSolution {
publicint[][]constructProductMatrix(int[][] grid) {
int n = grid.length, m = grid[0].length, sz = n * m;
int[] flat =newint[sz];
int k = 0;
for (int[] row : grid) for (int x : row) flat[k++]= x;
int[] pre =newint[sz+1], suf =newint[sz+1];
pre[0]= suf[sz]= 1;
for (int i = 0; i < sz; i++) pre[i+1]= pre[i]* flat[i]% 12345;
for (int i = sz-1; i >= 0; i--) suf[i]= suf[i+1]* flat[i]% 12345;
int[][] ans =newint[n][m];
for (int i = 0; i < sz; i++) ans[i/m][i%m]= pre[i]* suf[i+1]% 12345;
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funconstructProductMatrix(grid: Array<IntArray>): Array<IntArray> {
val n = grid.size; val m = grid[0].size; val sz = n * m
val flat = IntArray(sz)
var k = 0for (row in grid) for (x in row) flat[k++] = x
val pre = IntArray(sz+1) { 1 }; val suf = IntArray(sz+1) { 1 }
for (i in0 until sz) pre[i+1] = pre[i] * flat[i] % 12345for (i in sz-1 downTo 0) suf[i] = suf[i+1] * flat[i] % 12345val ans = Array(n) { IntArray(m) }
for (i in0 until sz) ans[i/m][i%m] = pre[i] * suf[i+1] % 12345return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defconstructProductMatrix(self, grid: list[list[int]]) -> list[list[int]]:
n, m = len(grid), len(grid[0])
flat = [x for row in grid for x in row]
sz = n * m
pre = [1] * (sz +1)
suf = [1] * (sz +1)
for i in range(sz):
pre[i+1] = pre[i] * flat[i] %12345for i in range(sz-1, -1, -1):
suf[i] = suf[i+1] * flat[i] %12345 ans = [[0]*m for _ in range(n)]
for i in range(sz):
ans[i//m][i%m] = pre[i] * suf[i+1] %12345return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnconstruct_product_matrix(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = grid.len(); let m = grid[0].len(); let sz = n * m;
let flat: Vec<i32>= grid.iter().flat_map(|row| row.iter()).cloned().collect();
letmut pre =vec![1; sz+1];
letmut suf =vec![1; sz+1];
for i in0..sz { pre[i+1] = pre[i] * flat[i] %12345; }
for i in (0..sz).rev() { suf[i] = suf[i+1] * flat[i] %12345; }
letmut ans =vec![vec![0; m]; n];
for i in0..sz { ans[i/m][i%m] = pre[i] * suf[i+1] %12345; }
ans
}
}