-
Notifications
You must be signed in to change notification settings - Fork 0
/
0084.cpp
96 lines (90 loc) · 2.44 KB
/
0084.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution
{
public:
/*
// 双指针解法
int largestRectangleArea(vector<int>& heights)
{
vector<int> minLeftIndex(heights.size());
vector<int> minRightIndex(heights.size());
int size = heights.size();
// 是为了求出当前柱子高度所能覆盖的最大面积
// 记录每个柱子 左边第一个小于该柱子的下标
minLeftIndex[0] = -1;
for (int i = 1; i < size; i++)
{
int t = i - 1;
while (t >= 0 && heights[t] >= heights[i])
t = minLeftIndex[t];
minLeftIndex[i] = t;
}
// 记录每个柱子 右边第一个小于该柱子的下标
minRightIndex[size - 1] = size;
for (int i = size - 2; i >= 0; i--)
{
int t = i + 1;
while (t < size && heights[t] >= heights[i])
t = minRightIndex[t];
minRightIndex[i] = t;
}
// 求和
int ans = 0;
for (int i = 0; i < size; i++)
{
int sum = heights[i] * (minRightIndex[i] - minLeftIndex[i] - 1);
ans = max(ans, sum);
}
return ans;
}
*/
// 单调栈 勉强懂
int largestRectangleArea(vector<int>& heights)
{
if (heights.size() == 1)
return heights[0];
stack<int> st;
// 数组头部加入元素 0
heights.insert(heights.cbegin(), 0);
heights.push_back(0);
int size = heights.size();
st.push(0);
int ans = 0;
for (int i = 1; i < size; i++)
{
if (heights[st.top()] < heights[i])
st.push(i);
else if(heights[st.top()] == heights[i])
{
st.pop();
st.push(i);
}
else if (heights[st.top()] > heights[i])
{
while (!st.empty() && heights[st.top()] > heights[i])
{
int mid = st.top();
st.pop();
if (!st.empty())
{
int h = heights[mid];
int w = i - st.top() - 1;
ans = max(ans, h * w);
}
}
st.push(i);
}
}
return ans;
}
};
int main()
{
vector<int> heights = { 2,1,5,6,2,3 };
Solution S;
S.largestRectangleArea(heights);
return 0;
}