-
Notifications
You must be signed in to change notification settings - Fork 0
/
84.cpp
43 lines (40 loc) · 983 Bytes
/
84.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
43
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
class Solution
{
public:
int largestRectangleArea(vector<int> &heights)
{
if (heights.empty())
return 0;
if (heights.size() == 1)
return heights[0];
heights.emplace_back(-1);
int n = heights.size();
int maxArea = 0, i = 0;
stack<int> indexs;
while (i < n)
{
if (indexs.empty() || heights[i] >= heights[indexs.top()])
indexs.push(i++);
else
{
int j = indexs.top();
indexs.pop();
int cur = heights[j] * (indexs.empty() ? i : (i - indexs.top() - 1));
maxArea = max(maxArea, cur);
}
}
return maxArea;
}
};
int main()
{
vector<int> input = {4, 2, 0, 3, 2, 5};
Solution solution;
solution.largestRectangleArea(input);
system("pause");
return 0;
}