Prefix Sum V: Prefix && Suffix
Interesting problem, the hints guide you to avoid using the division (you'll run into TLE), solution here is to create a prefix product, a suffix product, and then determine the resulting matrix. Make use of the following modular identity:
(A*B)%M = ((A%M) * (B%M))%M
Code is down below, cheers, ACC.
Construct Product Matrix - LeetCode
2906. Construct Product Matrix
Medium
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 ingrid
except for the elementgrid[i][j]
. This product is then taken modulo12345
.
Return the product matrix of grid
.
Example 1:
Input: grid = [[1,2],[3,4]] Output: [[24,12],[8,6]] Explanation: p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24 p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12 p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8 p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6 So the answer is [[24,12],[8,6]].
Example 2:
Input: grid = [[12345],[2],[1]] Output: [[2],[0],[0]] Explanation: p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2. p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0. p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0. So the answer is [[2],[0],[0]].
Constraints:
1 <= n == grid.length <= 105
1 <= m == grid[i].length <= 105
2 <= n * m <= 105
1 <= grid[i][j] <= 109
Accepted
7,537
Submissions
26,026
public int[][] ConstructProductMatrix(int[][] grid) { int MOD = 12345; long[][] prefixProduct = new long[grid.Length][]; for (int r = 0; r < grid.Length; r++) prefixProduct[r] = new long[grid[r].Length]; for (int r = 0; r < grid.Length; r++) { for (int c = 0; c < grid[r].Length; c++) { int previousR = 0; int previousC = 0; previousC = c - 1; if (previousC < 0) { previousR = r - 1; previousC = grid[0].Length - 1; } else { previousR = r; } if (previousC >= 0 && previousR >= 0) { prefixProduct[r][c] = (prefixProduct[previousR][previousC] * grid[previousR][previousC]) % MOD; } else { prefixProduct[r][c] = 1; } } } long[][] suffixProduct = new long[grid.Length][]; for (int r = 0; r < grid.Length; r++) suffixProduct[r] = new long[grid[r].Length]; for (int r = grid.Length - 1; r >= 0; r--) { for (int c = grid[r].Length - 1; c >= 0; c--) { int nextR = 0; int nextC = 0; nextC = c + 1; if (nextC >= grid[0].Length) { nextR = r + 1; nextC = 0; } else { nextR = r; } if (nextC < grid[0].Length && nextR < grid.Length) { suffixProduct[r][c] = (suffixProduct[nextR][nextC] * grid[nextR][nextC]) % MOD; } else { suffixProduct[r][c] = 1; } } } int[][] retVal = new int[grid.Length][]; for (int r = 0; r < grid.Length; r++) retVal[r] = new int[grid[r].Length]; for (int r = 0; r < grid.Length; r++) { for (int c = 0; c < grid[r].Length; c++) { retVal[r][c] = ((int)prefixProduct[r][c] * (int)suffixProduct[r][c]) % MOD; } } return retVal; }
Comments
Post a Comment