-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1277 Count Square Submatrices with All Ones.cpp
More file actions
59 lines (48 loc) · 1.32 KB
/
1277 Count Square Submatrices with All Ones.cpp
File metadata and controls
59 lines (48 loc) · 1.32 KB
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
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int m=matrix.size();
int n=matrix[0].size();
int t[m][n];
memset(t,0,sizeof(t));
for(int i=0;i<m;i++){
t[i][0]=matrix[i][0];
}
for(int j=0;j<n;j++){
t[0][j]=matrix[0][j];
}
// for(int i=0;i<m;i++){
// for(int j=0;j<n;j++){
// cout<<t[i][j]<<" ";
// }
// cout<<endl;
// }
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
// considering t[i][j] is the bottom right corner for a square box
if(matrix[i][j]==1)
t[i][j]=1+min(t[i-1][j-1],min(t[i-1][j],t[i][j-1]));
}
}
//[[1,0,1,0,0],[1,0,1,1,1],[1,1,1,1,1],[1,0,0,1,0]]
cout<<endl;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout<<t[i][j]<<" ";
}
cout<<endl;
}
long long sum=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
sum+=t[i][j];
}
}
return sum;
}
};