-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution221.java
More file actions
40 lines (34 loc) · 1.35 KB
/
Copy pathSolution221.java
File metadata and controls
40 lines (34 loc) · 1.35 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
import java.util.Arrays;
public class Solution221 {
public int maximalSquare(char[][] matrix) {
int maxSquare = 0;
int m = matrix.length;
int n = matrix[0].length;
int minSide = Math.min(m, n);
for (int y = 0; y < m; y++) {
for (int x = 0; x < n; x++) {
char current = matrix[y][x];
if (x > 0 && y > 0) {
char left = matrix[y][x - 1];
char up = matrix[y - 1][x];
char diagonal = matrix[y - 1][x - 1];
int min = Math.min(Math.min(left, up), diagonal);
if (current == '1' && min > 0) {
current = (char) ++min;
} else {
current = (char) (current == '1' ? 1 : 0);
}
} else {
current = (char) (current == '1' ? 1 : 0);
}
if (current > maxSquare) maxSquare = current;
matrix[y][x] = current;
}
}
return maxSquare * maxSquare;
}
public static void main(String[] args) {
char[][] matrix = {{'1', '0', '1', '1', '1'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}};
System.out.println(new Solution221().maximalSquare(matrix));
}
}