forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStamping the Grid.cpp
62 lines (59 loc) · 1.81 KB
/
Stamping the Grid.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
56
57
58
59
60
61
62
class Solution {
public:
bool possibleToStamp(vector<vector<int>>& grid, int stampHeight, int stampWidth) {
h = stampHeight;
w = stampWidth;
init(grid);
vector<vector<int>> diff(m+1, vector<int>(n+1, 0)); // 1-indexed
for (int i = m-1; i >= 0; i--) {
for (int j = n-1; j >= 0; j--) {
if (canStamp(i, j)) {
diff[i+1][j+1] ++;
diff[i+1][j+1-w] --;
diff[i+1-h][j+1] --;
diff[i+1-h][j+1-w] ++;
}
}
}
int cur = 0;
for (int i = m-1; i >= 0; i--) {
if (i < m-1) {
for (int j = 0; j <= n; j++)
diff[i+1][j] += diff[i+2][j];
}
for (int j = n-1; j >= 0; j--) {
cur += diff[i+1][j+1];
if (cur > 0) {
grid[i][j] = 1;
}
}
cur += diff[i+1][0];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != 1) return false;
}
}
return true;
}
private:
vector<vector<int>> pre;
int h;
int w;
int m;
int n;
void init(vector<vector<int>>& grid) {
m = grid.size(), n = grid[0].size();
// initialize prefix sum
pre.resize(m+1, vector<int>(n+1, 0)); // 1-indexed
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
pre[i+1][j+1] = pre[i+1][j] + pre[i][j+1] - pre[i][j] + grid[i][j];
}
}
}
bool canStamp(int i, int j) {
if (i+1 < h || j+1 < w) return false;
return pre[i+1][j+1] - pre[i+1][j+1-w] - pre[i+1-h][j+1] + pre[i+1-h][j+1-w] == 0;
}
};