-
Notifications
You must be signed in to change notification settings - Fork 0
/
84-柱状图中最大的矩形.cpp
70 lines (70 loc) · 2.26 KB
/
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
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
/*
* @lc app=leetcode.cn id=84 lang=cpp
*
* [84] 柱状图中最大的矩形
*/
#include <algorithm>
#include <climits>
#include <memory>
#include <stack>
#include <vector>
using namespace std;
// @lc code=start
class Solution
{
public:
// 二维dp超时, dp[i][j] 是heights[i]到heights[j]间覆盖的矩形的面积
// int largestRectangleArea(vector<int>& heights)
// {
// int n = heights.size();
// vector<vector<int>> dp(n, vector<int>(n, 0));
// int maxS = INT_MIN;
// for (int i = 0; i < n; i++) {
// dp[i][i] = heights[i];
// maxS = max(maxS, dp[i][i]);
// }
// for (int l = 2; l <= n; l++) {
// for (int i = 0; i < n; i++) {
// int j = i + l - 1;
// if (j < n) {
// int h = dp[i][j - 1] / (j - i);
// if (heights[j] >= h)
// dp[i][j] = h * l;
// else
// dp[i][j] = heights[j] * l;
// maxS = max(maxS, dp[i][j]);
// }
// }
// }
// return maxS;
// }
// 关键是找到每一个高度左边第一个低于该高度的下标和右边第一个低于该高度的下标
int largestRectangleArea(vector<int>& heights)
{
int n = heights.size();
// 保存小于heights[i]的左边第一个数的下标
vector<int> left_low(n, -1);
vector<int> right_low(n, -1);
// 栈顶是与当前下标最接近的,小于当前值的下标
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && heights[st.top()] >= heights[i])
st.pop();
left_low[i] = st.empty() ? -1 : st.top();
st.push(i);
}
st = stack<int>();
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && heights[st.top()] >= heights[i])
st.pop();
right_low[i] = st.empty() ? n : st.top();
st.push(i);
}
int maxS = INT_MIN;
for (int i = 0; i < n; i++) {
maxS = max(maxS, heights[i] * (right_low[i] - left_low[i] - 1));
}
return maxS;
}
};
// @lc code=end