-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSquareSubmatrices.java
More file actions
47 lines (31 loc) · 1.14 KB
/
Copy pathCountSquareSubmatrices.java
File metadata and controls
47 lines (31 loc) · 1.14 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
39
40
41
42
43
44
45
46
47
package com.rbhatt.dynamicprogramming;
//Array
//dynamic programming
//Leetcode question # 1277
public class CountSquareSubmatrices {
public int countSquares(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int result = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
continue;
}
if (i > 0 && j > 0) {
matrix[i][j] = Math.min(Math.min(matrix[i][j-1], matrix[i-1][j]), matrix[i-1][j-1]) + 1;
}
result += matrix[i][j];
}
}
return result;
}
public static void main(String[] args) {
CountSquareSubmatrices ob = new CountSquareSubmatrices();
int[][] matrix = new int[3][4];
matrix[0][0] = 0; matrix[0][1] = 1; matrix[0][2] = 1; matrix[0][3] = 1;
matrix[1][0] = 1; matrix[1][1] = 1; matrix[1][2] = 1; matrix[1][3] = 1;
matrix[2][0] = 0; matrix[2][1] = 1; matrix[2][2] = 1; matrix[2][3] = 1;
System.out.println(ob.countSquares(matrix));
}
}