forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestRectangle.cpp
37 lines (31 loc) · 898 Bytes
/
LargestRectangle.cpp
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
//Find largest rectangle in histogram
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
vector<int> nsl(n),psl(n);
stack<int> s;
for(int i=n-1;i>=0;i--){
while(!s.empty() && heights[s.top()]>=heights[i]){
s.pop();
}
nsl[i] = s.empty() ? n : s.top();
s.push(i);
}
while(!s.empty()){
s.pop();
}
for(int i=0;i<n;i++){
while(!s.empty() && heights[s.top()]>=heights[i]){
s.pop();
}
psl[i] = s.empty() ? -1: s.top();
s.push(i);
}
int ans = INT_MIN;
for(int i=0;i<n;i++){
ans = max(ans,(nsl[i]-psl[i]-1)*(heights[i]));
}
return ans;
}
};