forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1277_Count_square_submatrics_with_ones
35 lines (34 loc) · 1.07 KB
/
1277_Count_square_submatrics_with_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
// https://leetcode.com/problems/count-square-submatrices-with-all-ones/
class Solution {
public int countSquares(int[][] A) {
int res = 0;
for (int i = 0; i < A.length; ++i) {
for (int j = 0; j < A[0].length; ++j) {
if (A[i][j] > 0 && i > 0 && j > 0) {
A[i][j] = Math.min(A[i - 1][j - 1], Math.min(A[i - 1][j], A[i][j - 1])) + 1;
}
res += A[i][j];
}
}
return res;
}
}
// class Solution {
// public int countSquares(int[][] matrix) {
// int max = 0;
// for(int i=0;i<matrix.length-1;i++) {
// int count = 0;
// for(int j=0;j<matrix[0].length-1;j++) {
// for(int k=0;k<matrix.length-1;k++) {
// if(matrix[i+k][j+k]==1)
// {
// count++;
// if(max<count)
// max = count;
// }
// }
// }
// }
// return max;
// }
// }