单调栈
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int Len = heights.size();
vector<int> sta, L(Len, 0), R(Len, 0);
for (int i = 0; i < Len; ++i) {
while (!sta.empty() && heights[sta.back()] > heights[i]) {
int x = sta.back(); sta.pop_back();
R[x] = i - 1;
}
sta.push_back(i);
}
while (!sta.empty()) {
int x = sta.back(); sta.pop_back();
R[x] = Len - 1;
}
for (int i = Len - 1; i >= 0; --i) {
while (!sta.empty() && heights[sta.back()] > heights[i]) {
int x = sta.back(); sta.pop_back();
L[x] = i + 1;
}
sta.push_back(i);
}
while (!sta.empty()) {
int x = sta.back(); sta.pop_back();
L[x] = 0;
}
int ans = 0;
for (int i = 0; i < Len; ++i)
ans = max(ans, (R[i] - L[i] + 1)*heights[i]);
return ans;
}
};