-
Notifications
You must be signed in to change notification settings - Fork 1
/
Count Square Submatrices with All Ones
58 lines (53 loc) · 1.54 KB
/
Count Square Submatrices with All Ones
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
class Solution {
public int countSquares(int[][] matrix) {
/*
int count = 0;
for(int i = 1; i <= matrix.length; i++){
for(int j = 0; j < matrix.length; j++){
for(int z = 0; z < matrix[0].length; z++){
if(isSquare(matrix, i, j, z)){
count++;
}
}
}
}
return count;
*/
if(matrix.length == 0){
return 0;
}
int r = matrix.length;
int c = matrix[0].length;
int[][] dp = new int[r][c];
int count = 0;
for(int i = 0; i<r; i++){
for(int j = 0; j<c; j++){
if(matrix[i][j] == 1){
if(i == 0 || j == 0){
dp[i][j] = 1;
}
else{
dp[i][j] = Math.min(Math.min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1;
}
}
count += dp[i][j];
}
}
return count;
}
/*
public boolean isSquare(int[][] matrix, int sideL, int row, int col){
if(row + sideL > matrix.length || col + sideL > matrix[0].length){
return false;
}
for(int i = row; i < row + sideL; i++){
for(int j = col; j < col + sideL; j++){
if(matrix[i][j] == 0){
return false;
}
}
}
return true;
}
*/
}