-
Notifications
You must be signed in to change notification settings - Fork 0
/
85. 最大矩形.java
49 lines (47 loc) · 1.43 KB
/
85. 最大矩形.java
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
class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0) {
return 0;
}
int m = matrix.length;
int n = matrix[0].length;
int[] height = new int[n];
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(right, n);
int maxarea = 0;
for (int i = 0; i < m; i++) {
int cur_left = 0, cur_right = n;
// update left
for (int j = 0; j < n; j++) {
if (matrix[i][j] == '1') {
left[j] = Math.max(left[j], cur_left);
} else {
left[j] = 0;
cur_left = j + 1;
}
}
// update right
for (int j = n - 1; j >= 0; j--) {
if (matrix[i][j] == '1') {
right[j] = Math.min(right[j], cur_right);
} else {
right[j] = n;
cur_right = j;
}
}
// update height
for (int j = 0; j < n; j++) {
if (matrix[i][j] == '1') {
height[j]++;
} else {
height[j] = 0;
}
}
for (int j = 0; j < n; j++) {
maxarea = Math.max(maxarea, height[j] * (right[j] - left[j]));
}
}
return maxarea;
}
}