forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0084.cpp
More file actions
25 lines (24 loc) · 729 Bytes
/
0084.cpp
File metadata and controls
25 lines (24 loc) · 729 Bytes
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
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int largestRectangleArea(vector<int>& heights)
{
vector<int> stack;
int res = 0;
heights.push_back(-1);
for (int i = 0; i < heights.size(); ++i)
{
while (!stack.empty() and heights[*(stack.rbegin())] >= heights[i])
{
int height_id = *(stack.rbegin()); stack.pop_back();
int pre_id = stack.empty() ? -1 : *(stack.rbegin());
res = max(res, heights[height_id]*(i - pre_id - 1));
}
stack.push_back(i);
}
return res;
}
};