-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount square submatrix with all ones(Leetcode).cpp
50 lines (40 loc) · 1.24 KB
/
Count square submatrix with all ones(Leetcode).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
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int result=0;
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[0].size();j++){
if(i>0 && j>0 && matrix[i][j]>0)
matrix[i][j] = min(matrix[i-1][j-1], min(matrix[i-1][j], matrix[i][j-1])) + 1;
result += matrix[i][j];
}
}
return result;
}
};
/*
Let the matrix be
1 1
1 1
There are 5 squares in total 4 of side 1 and 1 of side 2
Logic I have used:
if the current cell(i,j) is not 0, check for the min element in the left, right, diagonal element and add it to the value of current cell
else leave it as it is as there is no edge
After applying this logic matrix becomes
1 1
1 2
sumof all the elements in the matrix results in the total number of squares
*/
// int countSquares(vector<vector<int>>& matrix) {
// int m=matrix.size(),n=matrix[0].size();
// for(int i=1;i<m;i++){
// for(int j=1;j<n;j++){
// if(matrix[i][j]!=0) matrix[i][j]+=min({matrix[i-1][j],matrix[i][j-1],matrix[i-1][j-1]});
// }
// }
// int cnt=0;
// for(int i=0;i<m;i++){
// for(int j=0;j<n;j++) cnt+=matrix[i][j];
// }
// return cnt;
// }