forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct-product-matrix.cpp
55 lines (51 loc) · 1.85 KB
/
construct-product-matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Time: O(m * n)
// Space: O(m * n)
// prefix sum
class Solution {
public:
vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
static const int MOD = 12345;
vector<int64_t> right(size(grid) * size(grid[0]) + 1, 1);
for (int i = size(grid) - 1; i >= 0; --i) {
for (int j = size(grid[0]) - 1; j >= 0; --j) {
right[i * size(grid[0]) + j] = (right[(i * size(grid[0]) + j) + 1] * grid[i][j]) % MOD;
}
}
int64_t left = 1;
for (int i = 0; i < size(grid); ++i) {
for (int j = 0; j < size(grid[0]); ++j) {
const int x = grid[i][j];
grid[i][j] = (left * right[(i * size(grid[0]) + j) + 1]) % MOD;
left = (left * x) % MOD;
}
}
return grid;
}
};
// Time: O(m * n)
// Space: O(m * n)
// prefix sum
class Solution2 {
public:
vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
static const int MOD = 12345;
vector<int64_t> left(size(grid) * size(grid[0]) + 1, 1);
for (int i = 0; i < size(grid); ++i) {
for (int j = 0; j < size(grid[0]); ++j) {
left[(i * size(grid[0]) + j) + 1] = (left[i * size(grid[0]) + j] * grid[i][j]) % MOD;
}
}
vector<int64_t> right(size(grid) * size(grid[0]) + 1, 1);
for (int i = size(grid) - 1; i >= 0; --i) {
for (int j = size(grid[0]) - 1; j >= 0; --j) {
right[i * size(grid[0]) + j] = (right[(i * size(grid[0]) + j) + 1] * grid[i][j]) % MOD;
}
}
for (int i = 0; i < size(grid); ++i) {
for (int j = 0; j < size(grid[0]); ++j) {
grid[i][j] = (left[i * size(grid[0]) + j] * right[(i * size(grid[0]) + j) + 1]) % MOD;
}
}
return grid;
}
};