- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution84.java
More file actions
Latest commit
executable file
·27 lines (24 loc) · 781 Bytes
/
Copy pathSolution84.java
File metadata and controls
executable file
·27 lines (24 loc) · 781 Bytes
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
importjava.util.Stack;
/**
* Created by slade on 2019/7/8.
*/
publicclassSolution84 {
publicintlargestRectangleArea(int[] heights) {
Stack<Integer> stack = newStack<>();
stack.push(-1);
intheightsLen = heights.length;
intarea = 0;
for (inti = 0; i < heightsLen; i++) {
while (stack.peek() != -1 && heights[stack.peek()] >= heights[i]) {
intcurIdx = stack.pop();
area = Math.max(area, heights[curIdx] * (i - stack.peek() - 1));
}
stack.push(i);
}
while (stack.peek() != -1) {
intcurIdx = stack.pop();
area = Math.max(area, heights[curIdx] * (heightsLen - stack.peek() - 1));
}
returnarea;
}
}