forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
42 lines (38 loc) · 1.11 KB
/
Solution.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
38
39
40
41
42
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
if(heights.empty())return 0;
int len = heights.size();
stack<int> s_stack;
int ans = 0;
for(int i = 0;i < len;i++){
if(s_stack.empty() || heights[i] >= s_stack.top())
{//满足升序条件
s_stack.push(heights[i]);
}
else
{//不满足升序
int count = 0;
while(!s_stack.empty() && s_stack.top() > heights[i])
{
count++;
ans = max(ans,s_stack.top() * count);
s_stack.pop();
}
while(count > 0)
{
s_stack.push(heights[i]);
count--;
}
s_stack.push(heights[i]);
}
}
int count = 1;
while(!s_stack.empty()){
ans = max(ans,s_stack.top() * count);
s_stack.pop();
count++;
}
return ans;
}
};