-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-submatrices-with-equal-frequency-of-x-and-y.cpp
56 lines (54 loc) · 1.93 KB
/
count-submatrices-with-equal-frequency-of-x-and-y.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
// Time: O(n * m)
// Space: O(n * m)
// dp
class Solution {
public:
int numberOfSubmatrices(vector<vector<char>>& grid) {
int result = 0;
vector<vector<int>> dp1(size(grid) + 1, vector<int>(size(grid[0]) + 1));
vector<vector<int>> dp2(size(grid) + 1, vector<int>(size(grid[0]) + 1));
for (int i = 0; i < size(grid); ++i) {
for (int j = 0; j < size(grid[0]); ++j) {
dp1[i + 1][j + 1] = dp1[i][j + 1] + dp1[i + 1][j] - dp1[i][j] + (grid[i][j] == 'X' ? 1 : 0);
dp2[i + 1][j + 1] = dp2[i][j + 1] + dp2[i + 1][j] - dp2[i][j] + (grid[i][j] == 'Y' ? 1 : 0);
if (dp1[i + 1][j + 1] == dp2[i + 1][j + 1] && dp2[i + 1][j + 1] != 0) {
++result;
}
}
}
return result;
}
};
// Time: O(n * m)
// Space: O(n * m)
// dp
class Solution2 {
public:
int numberOfSubmatrices(vector<vector<char>>& grid) {
int result = 0;
vector<vector<int>> dp1(size(grid), vector<int>(size(grid[0])));
vector<vector<int>> dp2(size(grid), vector<int>(size(grid[0])));
for (int i = 0; i < size(grid); ++i) {
for (int j = 0; j < size(grid[0]); ++j) {
if (i - 1 >= 0) {
dp1[i][j] += dp1[i - 1][j];
dp2[i][j] += dp2[i - 1][j];
}
if (j - 1 >= 0) {
dp1[i][j] += dp1[i][j - 1];
dp2[i][j] += dp2[i][j - 1];
}
if (i - 1 >= 0 && j - 1 >= 0) {
dp1[i][j] -= dp1[i - 1][j - 1];
dp2[i][j] -= dp2[i - 1][j - 1];
}
dp1[i][j] += (grid[i][j] == 'X' ? 1 : 0);
dp2[i][j] += (grid[i][j] == 'Y' ? 1 : 0);
if (dp1[i][j] == dp2[i][j] && dp2[i][j] != 0) {
++result;
}
}
}
return result;
}
};