- 84.柱状图中最大的矩形
```cpp
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> st;
st.push(0); // 存下标
// 头尾存0,分别处理heights 全程递减和递增的情况
heights.insert(heights.begin(), 0);
heights.push_back(0);
int result = 0;
for (int i = 1; i < heights.size(); i++) {
if (heights[i] >= heights[st.top()]) st.push(i);
else {
while (!st.empty() && heights[i] < heights[st.top()]) {
int index = st.top();
st.pop();
if (!st.empty()) {
int w = i - st.top() - 1;
int h = heights[index];
result = max(result, h * w);
}
}
st.push(i);
}
}
return result;
}
};
```