-
Notifications
You must be signed in to change notification settings - Fork 0
/
_084.cpp
27 lines (26 loc) · 847 Bytes
/
_084.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
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> s; int max1 = 0;
for(int i = 0;i < heights.size();i++){
if(s.empty() || heights[i] > heights[s.top()]) s.push(i);
else{
while(!s.empty() && heights[i] <= heights[s.top()]){
int index = s.top();
s.pop();
int left = s.empty()?-1:s.top();
max1 = max(max1,(i-left-1)*heights[index]);
}
s.push(i);
}
}
while(!s.empty()){
int index = s.top();
s.pop();
int left = s.empty()?-1:s.top();
int l = heights.size()-1-left;
max1 = max(max1,l*heights[index]);
}
return max1;
}
};