-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0084.java
More file actions
37 lines (33 loc) · 1.03 KB
/
Copy pathLeetCode0084.java
File metadata and controls
37 lines (33 loc) · 1.03 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
/* Largest Rectangle in Histogram
* Example:
* Input: [2,1,5,6,2,3]
* Output: 10
* */
import java.util.Stack;
public class LeetCode0084 {
public static void main(String args[]) {
int[] height = {2, 1, 5, 6, 4, 3};
System.out.println(largestRectangleArea(height));
}
public static int largestRectangleArea(int[] heights) {
int len = heights.length;
//单调递增栈
Stack<Integer> stack = new Stack<>();
stack.push(-1);
int max = 0;
for (int i = 0; i < len; ++i) {
while (stack.peek() != -1 && heights[i] < heights[stack.peek()]) {
int h = heights[stack.pop()];
int w = i - stack.peek() - 1;
max = Math.max(max, w * h);
}
stack.push(i);
}
while (stack.peek() != -1) {
int h = heights[stack.pop()];
int w = len - stack.peek() - 1;
max = Math.max(max, w * h);
}
return max;
}
}