-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximalRectangle.java
69 lines (61 loc) · 1.82 KB
/
MaximalRectangle.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution {
class Node {
private int index; // min index can cover
private int value;
public Node(int index, int value) {
this.index = index;
this.value = value;
}
}
public int maximalRectangle(int[] h) {
int m = h.length;
Stack<Node> st = new Stack<>();
int result = 0;
for (int j = 0; j < m; j++) {
int index = j;
while (!st.isEmpty() && st.peek().value >= h[j]) {
Node node = st.pop();
int area = (j - node.index) * node.value;
result = Math.max(result, area);
index = node.index;
}
if (h[j] > 0) {
st.add(new Node(index, h[j]));
}
}
while (!st.isEmpty()) {
Node node = st.pop();
int area = (m - node.index) * node.value;
result = Math.max(result, area);
}
return result;
}
public int maximalRectangle(char[][] matrix) {
int n = matrix.length;
if (n == 0) {
return 0;
}
int m = matrix[0].length;
if (m == 0) {
return 0;
}
int[][] h = new int[n][m];
for (int j = 0; j < m; j++) {
h[0][j] = matrix[0][j] == '1' ? 1 : 0;
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == '1') {
h[i][j] = h[i - 1][j] + 1;
} else {
h[i][j] = 0;
}
}
}
int result = 0;
for (int i = 0; i < n; i++) {
result = Math.max(result, maximalRectangle(h[i]));
}
return result;
}
}