-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathLargest_rectangle_in_histogram.java
38 lines (38 loc) · 1.09 KB
/
Largest_rectangle_in_histogram.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
class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> s=new Stack<>();
int nsl[]=new int[heights.length];
int nsr[]=new int[heights.length];
// finfing Next Smaller To Left
for(int i=0;i<heights.length;i++){
while(!s.isEmpty() && heights[s.peek()]>=heights[i]){
s.pop();
}
if(s.isEmpty()){
nsl[i]=-1;
} else {
nsl[i]=s.peek();
}
s.push(i);
}
s = new Stack<>();
// finding Next Smaller To Right
for(int i=heights.length-1;i>=0;i--){
while(!s.isEmpty() && heights[s.peek()]>=heights[i]){
s.pop();
}
if(s.isEmpty()){
nsr[i]=heights.length;
} else {
nsr[i]=s.peek();
}
s.push(i);
}
int ans=0;
for(int i=0;i<heights.length;i++){
int area=(nsr[i]-nsl[i]-1)*heights[i];
ans=Math.max(ans,area);
}
return ans;
}
}