-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_85.java
More file actions
42 lines (38 loc) · 1.22 KB
/
Copy pathP_85.java
File metadata and controls
42 lines (38 loc) · 1.22 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
package leetcode.hard;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
public class P_85 {
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0) {
return 0;
}
int res = 0;
final int m = matrix[0].length;
final int[] curr = new int[m];
for (char[] row : matrix) {
for (int j = 0; j < m; j++) {
if (row[j] == '0') {
curr[j] = 0;
} else {
curr[j]++;
}
}
res = Math.max(res, largestRectangleArea(curr));
}
return res;
}
public int largestRectangleArea(int[] heights) {
final Deque<Integer> stack = new ArrayDeque<>(Collections.singleton(-1));
int res = 0;
for (int i = 0; i <= heights.length; i++) {
while (stack.size() > 1 && (i == heights.length || heights[stack.getFirst()] > heights[i])) {
final int top = stack.removeFirst();
final int area = heights[top] * (i - stack.getFirst() - 1);
res = Math.max(res, area);
}
stack.addFirst(i);
}
return res;
}
}