-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathLaregstAreaOfHistorgam.java
46 lines (41 loc) · 1.31 KB
/
LaregstAreaOfHistorgam.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
class Solution {
public int largestRectangleArea(int[] heights) {
if(heights == null || heights.length == 0)
return 0;
int n = heights.length;
int[] left = new int[n];
int[] right = new int[n];
Stack<Integer> stack = new Stack<>();
//using the stack to find the last number which smaller than heights[i]
for(int i=0; i<n; i++) {
while(!stack.isEmpty() && heights[i] <= heights[stack.peek()]) {
stack.pop();
}
if(stack.isEmpty()) {
left[i] = -1;
} else {
left[i] = stack.peek();
}
stack.push(i);
}
stack.clear();
//using the stack to find the next number which smaller than heights[i]
for(int i=n-1; i>=0; i--) {
while(!stack.isEmpty() && heights[i] <= heights[stack.peek()]) {
stack.pop();
}
if(stack.isEmpty()) {
right[i] = n;
} else {
right[i] = stack.peek();
}
stack.push(i);
}
int maxArea = 0;
for(int i=0; i<n; i++) {
int area = (right[i] - left[i] - 1) * heights[i];
maxArea = Math.max(maxArea, area);
}
return maxArea;
}
}