Class Solution
-
- All Implemented Interfaces:
public final class Solution
2906 - Construct Product Matrix.
Medium
Given a 0-indexed 2D integer matrix
grid
of sizen * m
, we define a 0-indexed 2D matrixp
of sizen * m
as the product matrix ofgrid
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: p0 = grid1 * grid0 * grid1 = 2 * 3 * 4 = 24
p1 = grid0 * grid0 * grid1 = 1 * 3 * 4 = 12
p0 = grid0 * grid1 * grid1 = 1 * 2 * 4 = 8
p1 = grid0 * grid1 * grid0 = 1 * 2 * 3 = 6
So the answer is \[\[24,12],8,6].
Example 2:
Input: grid = \[\[12345],2,1]
Output: \[\[2],0,0]
Explanation: p0 = grid1 * grid2 = 2 * 1 = 2.
p1 = grid0 * grid2 = 12345 * 1 = 12345. 12345 % 12345 = 0. So p1 = 0.
p2 = grid0 * grid1 = 12345 * 2 = 24690. 24690 % 12345 = 0. So p2 = 0.
So the answer is \[\[2],0,0].
Constraints:
<code>1 <= n == grid.length <= 10<sup>5</sup></code>
<code>1 <= m == gridi.length <= 10<sup>5</sup></code>
<code>2 <= n * m <= 10<sup>5</sup></code>
<code>1 <= gridj<= 10<sup>9</sup></code>