-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountSquareSubmatricesWithAllOnes.cpp
61 lines (57 loc) · 1.63 KB
/
CountSquareSubmatricesWithAllOnes.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
class Solution {
public:
int getmin(int a, int b, int c){
return min(a, min(b,c));
}
int countSquares(vector<vector<int>>& matrix) {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int res = 0;
int n = matrix.size();
int m = matrix[0].size();
vector<vector<int>> dp(n+1, vector<int>(m+1,0));
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
if(matrix[i][j] == 1){
dp[i+1][j+1] += matrix[i][j] + getmin(dp[i][j], dp[i][j+1], dp[i+1][j]);
res += dp[i+1][j+1];
}
}
}
return res;
}
};
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int res = 0;
int n = matrix.size();
int m = matrix[0].size();
vector<vector<int>> dp(n, vector<int>(m, 0));
for(int i = 0; i<matrix.size(); i++){
for(int j = 0; j<matrix[0].size(); j++){
if(matrix[i][j] == 1){
dp[i][j] = 1;
}
}
}
for(int i = 1; i<n; i++){
for(int j = 1; j<m; j++){
if(dp[i][j] == 1){
dp[i][j] = min(dp[i][j-1], min(dp[i-1][j-1], dp[i-1][j])) + 1;
}else{
dp[i][j] = 0;
}
}
}
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
cout<<dp[i][j]<<" ";
res += dp[i][j];
}
cout<<endl;
}
return res;
}
};