-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path084.cpp
32 lines (32 loc) · 1 KB
/
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
28
29
30
31
32
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int len = heights.size();
// 使用栈来保存一个递增的序列
stack<int> s;
int res = 0;
for(int i = 0; i < len; i++)
{
while(!s.empty() && heights[s.top()] > heights[i])
{
int tem = s.top();
s.pop();
// 计算不在序列内的区块的面积
int curArea = s.empty() ? i*heights[tem] : (i - s.top() - 1)*heights[tem];
res = max(res, curArea);
// cout<<i<<" "<<res<<endl;
}
s.push(i);
}
// 对递增序列中的序列面积进行计算
while(!s.empty())
{
int tem = s.top();
s.pop();
int curArea = s.empty() ? len*heights[tem] : (len - s.top() - 1) * heights[tem];
res = max(res, curArea);
// cout<<tem<<" "<<res<<endl;
}
return res;
}
};