文档链接:https://programmercarl.com/
LeetCode84.柱状图中的最大矩形
题目链接:https://leetcode.cn/problems/largest-rectangle-in-histogram/
思路:跟接雨水很相似
单调栈:
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> st;
int result = 0;
heights.insert(heights.begin(), 0);
heights.push_back(0);
st.push(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 mid = st.top();
st.pop();
if(!st.empty()) {
int left = st.top();
int h = heights[mid];
int w = i - left - 1;
result = max(result, h * w);
}
}
st.push(i);
}
}
return result;
}
};
总结:完结撒花!没想到两个月那么快,也没想到我这次真的能坚持下来。现在看来,我并没有蜕变,总结一下原因,自己也没有认认真真,老老实实的每一道题都搞明白,彻彻底底搞懂,反而很多题都是看了卡哥的讲解,自己照猫画虎AC了也就过了。非要说有什么突飞猛进的话也不太可能,尽管现在的我并没有当初加入训练营之前幻想的自己那么强,但收获总还是有的,也让我明白了自己和他人的差距,不过更重要的还是明白了刷题不是一时的,而是需要长期坚持的。