-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount Square Submatrices Dp.cpp
More file actions
38 lines (29 loc) · 1 KB
/
Copy pathCount Square Submatrices Dp.cpp
File metadata and controls
38 lines (29 loc) · 1 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
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int dp[matrix.size()][matrix[0].size()];
memset(dp,0,sizeof(dp));
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[0].size();j++){
if(i==0 || j==0){
dp[i][j]=matrix[i][j];
}else{
if(matrix[i][j]==0){
continue;
}
dp[i][j]=matrix[i][j]+ min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1]));
}
}
}
int ans=0;
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[0].size();j++){
cout<<dp[i][j]<<" ";
ans+=dp[i][j];
}
cout<<endl;
}
return ans;
}
};
//https://leetcode.com/problems/count-square-submatrices-with-all-ones/