Skip to content

Commit cf9bbed

Browse files
Sync LeetCode submission Runtime - 76 ms (24.54%), Memory - 150.3 MB (27.11%)
1 parent 332ec95 commit cf9bbed

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>Given a <strong>0-indexed</strong> 2D integer matrix <code><font face="monospace">grid</font></code><font face="monospace"> </font>of size <code>n * m</code>, we define a <strong>0-indexed</strong> 2D matrix <code>p</code> of size <code>n * m</code> as the <strong>product</strong> matrix of <code>grid</code> if the following condition is met:</p>
2+
3+
<ul>
4+
<li>Each element <code>p[i][j]</code> is calculated as the product of all elements in <code>grid</code> except for the element <code>grid[i][j]</code>. This product is then taken modulo <code><font face="monospace">12345</font></code>.</li>
5+
</ul>
6+
7+
<p>Return <em>the product matrix of</em> <code><font face="monospace">grid</font></code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> grid = [[1,2],[3,4]]
14+
<strong>Output:</strong> [[24,12],[8,6]]
15+
<strong>Explanation:</strong> p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24
16+
p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12
17+
p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8
18+
p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6
19+
So the answer is [[24,12],[8,6]].</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> grid = [[12345],[2],[1]]
25+
<strong>Output:</strong> [[2],[0],[0]]
26+
<strong>Explanation:</strong> p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2.
27+
p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0.
28+
p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0.
29+
So the answer is [[2],[0],[0]].</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= n == grid.length&nbsp;&lt;= 10<sup>5</sup></code></li>
36+
<li><code>1 &lt;= m == grid[i].length&nbsp;&lt;= 10<sup>5</sup></code></li>
37+
<li><code>2 &lt;= n * m &lt;= 10<sup>5</sup></code></li>
38+
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>9</sup></code></li>
39+
</ul>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
int MOD = 12345;
3+
public:
4+
vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
5+
int n = grid.size();
6+
int m = grid[0].size();
7+
vector<vector<long long>> res(n, vector<long long>(m));
8+
long long prod = 1;
9+
for (int i=0; i<n; i++) {
10+
for (int j=0; j<m; j++) {
11+
res[i][j] = prod;
12+
prod *= grid[i][j];
13+
prod %= MOD;
14+
}
15+
}
16+
prod = 1;
17+
for (int i=n-1; i>=0; i--) {
18+
for (int j=m-1; j>=0; j--) {
19+
res[i][j] *= prod;
20+
res[i][j] %= MOD;
21+
prod *= grid[i][j];
22+
prod %= MOD;
23+
}
24+
}
25+
vector<vector<int>> xd(n, vector<int>(m));
26+
for (int i=0; i<n; i++) {
27+
for (int j=0; j<m; j++) {
28+
xd[i][j] = res[i][j];
29+
}
30+
}
31+
return xd;
32+
}
33+
};

0 commit comments

Comments
 (0)