-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1277.cpp
More file actions
25 lines (24 loc) · 734 Bytes
/
1277.cpp
File metadata and controls
25 lines (24 loc) · 734 Bytes
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
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
if(matrix.empty())
return 0;
int r = matrix.size(), c = matrix[0].size();
vector<vector<int>> dp(r,vector<int>(c,0));
int res = 0;
for(int i=0;i<r; i++){
for(int j=0; j<c; j++){
if(matrix[i][j]==1)
res++;
if(i==0 || j==0)
dp[i][j] = matrix[i][j];
else if(matrix[i][j]==1){
int tmp = min({dp[i-1][j],dp[i][j-1],dp[i-1][j-1]});
dp[i][j] = tmp + 1;
res += tmp;
}
}
}
return res;
}
};