forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
35 lines (29 loc) · 1012 Bytes
/
Solution.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
class Solution {
public int largestRectangleArea(int[] heights) {
if (heights == null || heights.length == 0) {
return 0;
}
int n = heights.length;
if (n == 1) {
return heights[0];
}
// 创建一个新的数组,数组长度为 n + 1,最后一个元素值赋为 0
// 确保在后面的遍历中,原数组最后一个元素值能得到计算
int[] heightss = new int[n + 1];
heightss[n] = 0;
for (int i = 0; i < n; ++i) {
heightss[i] = heights[i];
}
Stack<Integer> stack = new Stack<>();
int max = 0;
for (int i = 0; i <= n;) {
if (stack.isEmpty() || heightss[i] > heightss[stack.peek()]) {
stack.push(i++);
} else {
int index = stack.pop();
max = Math.max(max, heightss[index] * (stack.isEmpty() ? i : i - stack.peek() - 1));
}
}
return max;
}
}