您的位置:首页 > 文旅 > 旅游 > 【c++刷题笔记-单调栈】day49: 42. 接雨水 、84.柱状图中最大的矩形

【c++刷题笔记-单调栈】day49: 42. 接雨水 、84.柱状图中最大的矩形

2024/10/6 18:26:20 来源:https://blog.csdn.net/qq_48662659/article/details/140658230  浏览:    关键词:【c++刷题笔记-单调栈】day49: 42. 接雨水 、84.柱状图中最大的矩形

42. 接雨水 - 力扣(LeetCode)

思路:单调栈,第一个是比栈顶大的是右边界,栈顶元素是底,出栈后下一个栈顶元素是左边界。

使用左右边界最小值与中间元素相减算出高度,右边界减左边界减1算出宽度

1.双指针

class Solution {
public:int trap(vector<int>& height) {int n=height.size();int ans=0;vector<int>left(n);vector<int>right(n);//左边第一个的左边最大数为0for(int i=1;i<n;i++){left[i]=max(left[i-1],height[i-1]);//预处理当前数左边的最大数}//右边第一个的右边最大数为0for(int i=n-2;i>=0;i--){right[i]=max(right[i+1],height[i+1]);//预处理当前数右边的最大数}for(int i=1;i<n-1;i++){int t=min(left[i],right[i]);//取最小的if(t>height[i]){//大于当前就算出可以接的雨水数量ans+=t-height[i];}}return ans;}
};

2.单调栈 

class Solution {
public:int trap(vector<int>& height) {int n=height.size();int ans=0;stack<int>st;st.push(0);for(int i=1;i<n;i++){while(!st.empty()&&height[i]>height[st.top()]){int mid=height[st.top()];//记录中间值st.pop();if(st.empty()){break;}int h=min(height[i],height[st.top()])-mid;//算出高度int w=i-st.top()-1;//减一算出宽度ans+=w*h;}st.push(i);} return ans;}
};

84. 柱状图中最大的矩形 - 力扣(LeetCode)

思路:同上,只是换成单调递减栈。以中间值为高度,右边界减左边界减1算出宽度。选最大的值

class Solution {
public:int largestRectangleArea(vector<int>& heights) {int ans=0;stack<int>st;//添加前导0和后导0判断原数组单调递增和单调递减的情况heights.insert(heights.begin(),0);heights.push_back(0);st.push(0);//注意是增加前导0和后导0的数组大小for(int i=1;i<heights.size();i++){//判断当前左右比当前小的值作为边界,当前数为高度while(heights[i]<heights[st.top()]){//单调递减int mid=st.top();st.pop();int w=i-st.top()-1;int h=heights[mid];ans=max(ans,w*h);}st.push(i);}return ans;}
};

总结

单调栈。在解决左边更大或右边更大的问题可以考虑单调栈方法

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com