-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximalRectangle.java
More file actions
83 lines (77 loc) · 2.56 KB
/
MaximalRectangle.java
File metadata and controls
83 lines (77 loc) · 2.56 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package hard;
import java.util.Stack;
import static util.Utils.*;
/**
*
* ClassName: MaximalRectangle
* @author chenyiAlone
* Create Time: 2019/01/30 16:00:52
* Description: No.85
* 总结:
* 1. 使用 heights[] 从上之下记录每行到首行的高度,转化为柱状图
* 2. stack 中维持升序,碰到逆序 pop 求取面积
*
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
Example:
Input:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6
*
*/
public class MaximalRectangle {
public int maximalRectangle(char[][] matrix) {
int res = 0;
int[] heights = new int[matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == '0') heights[j] = 0;
else heights[j]++;
}
printArray(heights);
res = Math.max(res, maxArceHistogram(heights));
}
return res;
}
public static int maxArceHistogram(int[] heights) {
int res = 0;
int[] nums = new int[heights.length + 1];
for (int i = 0; i < heights.length; i++) {
nums[i] = heights[i];
}
nums[heights.length] = 0;
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < nums.length; i++) {
if (stack.isEmpty() || nums[i] >= stack.peek()) {
stack.push(nums[i]);
} else {
int width = 1;
while (!stack.isEmpty() && nums[i] < stack.peek()) {
res = Math.max(res, width * stack.pop());
width++;
}
while (width-- >= 1) { // 至少push一个
stack.push(nums[i]);
}
}
}
System.out.println(res);
return res;
}
public static void main(String[] args) {
// int[] heights = {2, 1, 5, 6, 2, 3};
int[] heights = {3, 1, 3, 2, 2};
char[][] matrix = {
{'1','0','1','0','0'},
{'1','0','1','1','1'},
{'1','1','1','1','1'},
{'1','0','0','1','0'}
};
// System.out.println(new MaximalRectangle().maximalRectangle(matrix));
maxArceHistogram(heights);
}
}