代码随想录训练营第五十九天|503.下一个更大元素||、42.接雨水

news/2024/10/22 7:38:22/

503.下一个更大元素||

链接:LeetCode503下一个更大元素||

class Solution {
public:typedef pair<int,int> pa;vector<int> nextGreaterElements(vector<int>& nums) {int len = nums.size();for(int i=0;i<len;++i) nums.push_back(nums[i]);stack<pa> st;st.push(pa{nums[0],0});vector<int> ans(len,-1);for(int i=1;i<nums.size();++i){while(!st.empty()&&nums[i]>st.top().first){if(st.top().second<len)ans[st.top().second] = nums[i];st.pop();}st.push(pa{nums[i],i});}return ans;}
};

42.接雨水

链接:LeetCode42.接雨水

暴力解法(双指针)

按照列计算
在这里插入图片描述
按照列计算,宽度是1,此时把每一列的高度求出来就可以了。
每一列雨水的高度取决于该列左侧最高的柱子和右侧最高的柱子中最矮的那个柱子的高度。

class Solution {
public:int trap(vector<int>& height) {int ans = 0;for(int i=0;i<height.size();++i){int lheight = height[i];//左边柱子的最高高度int rheight = height[i];//右边柱子的最高高度if(i==0 || i==height.size()-1) continue;//第一根柱子和最后一根柱子不接雨水for(int j=i+1;j<height.size();++j) if(height[j]>rheight) rheight=height[j];for(int j=i-1;j>=0;--j) if(height[j]>lheight) lheight = height[j];int h = (lheight<rheight?lheight:rheight) - height[i];if(h>0) ans += h;}return ans;}
};

双指针优化

把每一个位置的左边最高高度记录在一个数组上maxLeft ,右边最高高度记录在一个数组上maxright.这样就避免了重复计算

class Solution {
public:int trap(vector<int>& height) {vector<int> maxlheight(height.size(),0);vector<int> maxrheight(height.size(),0);maxlheight[0] = height[0];for(int i=1;i<height.size();++i){maxlheight[i] = max(height[i],maxlheight[i-1]);}maxrheight[height.size()-1] = height[height.size()-1];for(int i=height.size()-2;i>=0;--i){maxrheight[i]=max(maxrheight[i+1],height[i]);}int ans = 0;for(int i=0;i<height.size();++i){if(i==0 || i==height.size()-1) continue;//第一根柱子和最后一根柱子不接雨水int h = min(maxlheight[i],maxrheight[i]) - height[i];if(h>0) ans += h;}return ans;}
};

单调栈

寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置,此时就要使用单调栈。

class Solution {
public:typedef pair<int,int>pa;int trap(vector<int>& height) {if(height.size()<3) return 0;int ans=0;stack<pa> st;for(int i=0;i<height.size();++i){while(!st.empty()&&height[i]>st.top().first){if(st.size()>=2){int mid = st.top().first;st.pop();// while(!st.empty()&&st.top().first<=mid) st.pop();// if(st.empty()) break;int le = st.top().first;int width = i-st.top().second-1;int len = (height[i]<le?height[i]:le) - mid;ans += width*len;//cout<<width<<"  "<<len<<"  "<<ans<<"  ";}else st.pop();}st.push(pa{height[i],i});}return ans;}
};

http://www.ppmy.cn/news/784469.html

相关文章

.net Core API 添加 NLog

nlog.config <?xml version"1.0" encoding"utf-8" ?> <nlog xmlns"http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"autoReload"true"internalLogLevel&quo…

Spyder输入中文后光标消失

在spyder中输入中文后&#xff0c;存在光标消失问题&#xff0c;可使用如下方式修正&#xff1a; 中文输入法状态&#xff0c;点击键盘任意一个字母&#xff0c;再点击backspace键&#xff0c;光标就会重新出现.

ubuntu光标消失

解决方案&#xff1a;echo -e “\033[?25h”

ue4当点击UI界面时,鼠标会消失不见

点击上图中的&#xff0c;打开关卡蓝图&#xff0c;在编辑器中&#xff0c;新增“Get Player Controller”,调用SetMouseCursor,勾选checkBox,

光标消失了要怎么调回来?

在打字的时候&#xff0c;我们偶尔会碰到光标从一条闪烁的竖线突然变成了下划线&#xff0c;或者直接整个消失的情况&#xff0c;那么遇见这种情况要怎么把光标变回原样呢&#xff1f; 苹果电脑按住Fn再按Enter(回车)键 win电脑按右上角Insert键 即可恢复&#xff0c;试一下呐…

解决Ubuntu 18.04 系统桌面鼠标光标消失的问题

最近安装Ubuntu 18.04的时候&#xff0c;遇到一个问题&#xff0c;具体表现是鼠标光标不见&#xff0c;并且无法恢复&#xff0c;我试着重新解压ISO文件也没有解决&#xff0c;而这个问题我找了挺多结果的&#xff0c;有重新安装驱动的&#xff0c;有说重新安装桌面的&#xff…

idea页面不显示鼠标光标了?_日记: router-view 页面不显示

今天学习Element 时候使用router-view 页面会不显示,而且也没有报错,后来发现2个问题 1.routes 的问题 import 这里的 routes 不是routers 也不是router . 2. 根元素问题 idapp <div idapp style"margin-top:10px"><router-view></router-view> &…