forked from kishanrajput23/leetcode-solutions-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximal square.java
More file actions
21 lines (20 loc) · 815 Bytes
/
maximal square.java
File metadata and controls
21 lines (20 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int maximalSquare(char[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int maxSquare = 0;
int[][] maxSideLength = new int[rows+1][cols+1];
for(int i=rows-1;i>=0;i--){
for(int j=cols-1;j>=0;j--){
if(matrix[i][j] == '1'){
// maximum sideLength of square is minimum of length covered by either of row, column, diagonal
maxSideLength[i][j] = Math.min(maxSideLength[i+1][j],Math.min(maxSideLength[i][j+1],maxSideLength[i+1][j+1]))+1;
maxSquare = Math.max(maxSquare, maxSideLength[i][j]);
}else{
maxSideLength[i][j] = 0;
}
}
}
return maxSquare*maxSquare;
}
}