-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1277.cpp
72 lines (65 loc) · 1.91 KB
/
1277.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
63
64
65
66
67
68
69
70
71
72
#include "leetcode.hpp"
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
auto m = matrix.size(), n = matrix[0].size();
int sum = 0;
auto dp = vector<vector<int>>(m, vector<int>(n, 0));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i == 0 || j == 0) {
dp[i][j] = matrix[i][j];
} else {
if (matrix[i][j] == 0) {
dp[i][j] = 0;
} else {
// transfer function
/**
* consider this case
*
* dp[i - 1][j - 1] = 5
* dp[i - 1][j] = 2
* dp[i][j - 1] = 3
*
* --------------------> j
* | 1 1 1 1 1
* | 1 1 1 1 1
* | 1 1 1 1 1
* | 1 1 1 1 1 1
* | 1 1 1 1 1 1
* ↓ 1 1 1 ?
* i
*/
dp[i][j] = 1 + min({dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]});
}
}
sum += dp[i][j];
}
}
return sum;
}
};
int main(int argc, char const* argv[]) {
Solution s;
vector<vector<int>> t1 =
{
{0, 1, 1, 1},
{1, 1, 1, 1},
{0, 1, 1, 1},
},
t2 =
{
{1, 0, 1},
{1, 1, 0},
{1, 1, 0},
},
t3 = {{0, 0, 0},
{0, 1, 0},
{0, 1, 0},
{1, 1, 1},
{1, 1, 0}};
auto ret = s.countSquares(t1); // 15
ret = s.countSquares(t2); // 7
ret = s.countSquares(t3); // 8
return 0;
}