当前位置: 首页 > news >正文

代码随想录训练营第六十三天打卡|503.下一个更大元素II 42. 接雨水

503.下一个更大元素II

1.暴力法,和每日温度那一题如出一辙,循环数组用了一个取模运算就解决了。

class Solution {
public:vector<int> nextGreaterElements(vector<int>& nums) {int n = nums.size();vector<int> result;for (int i = 0; i < n; i++) {int j;for (j = 1; j < n; j++) {if (nums[(i + j) % n] > nums[i]) {break;}}if (j == n)result.push_back(-1);elseresult.push_back(nums[(i + j) % n]);}return result;}
};

2.单调栈法。相比于之前的单调栈,多了循环数组的处理,很高兴一下子就想到了最简洁的写法。

class Solution {
public:vector<int> nextGreaterElements(vector<int>& nums) {int n = nums.size();stack<int> st;vector<int> result(n, -1);for (int i = 0; i < n * 2; i++) {while (!st.empty() && nums[i % n] > nums[st.top()]) {result[st.top()] = nums[i % n];st.pop();}st.push(i % n);}return result;}
};

42. 接雨水

知名hard题,自己想挑战一下,缝缝补补写了一个多小时依然只能通过四分之一左右的测试用例,遂放弃。

1.双指针优化版。本题的双指针解法其实还挺容易理解的。

class Solution {
public:int trap(vector<int>& height) {if (height.size() <= 2)return 0;vector<int> maxLeft(height.size(), 0);vector<int> maxRight(height.size(), 0);int size = maxRight.size();// 记录每个柱子左边柱子最大高度maxLeft[0] = height[0];for (int i = 1; i < size; i++) {maxLeft[i] = max(height[i], maxLeft[i - 1]);}// 记录每个柱子右边柱子最大高度maxRight[size - 1] = height[size - 1];for (int i = size - 2; i >= 0; i--) {maxRight[i] = max(height[i], maxRight[i + 1]);}// 求和int sum = 0;for (int i = 0; i < size; i++) {int count = min(maxLeft[i], maxRight[i]) - height[i];if (count > 0)sum += count;}return sum;}
};

2.单调栈,简洁写法。本题单调栈是按照行方向来计算雨水,这句话很关键。

class Solution {
public:int trap(vector<int>& height) {stack<int> st;int sum = 0;for (int i = 0; i < height.size(); i++) {while (!st.empty() && height[i] > height[st.top()]) {int mid = st.top();st.pop();if (!st.empty()) {int h = min(height[i], height[st.top()]) - height[mid];int w = i - st.top() - 1;sum += h * w;}}st.push(i);}return sum;}
};

3.单调栈。(注释版)理解了逻辑,代码并不复杂。

class Solution {
public:int trap(vector<int>& height) {if (height.size() <= 2)return 0; // 可以不加stack<int> st; // 存着下标,计算的时候用下标对应的柱子高度st.push(0);int sum = 0;for (int i = 1; i < height.size(); i++) {if (height[i] < height[st.top()]) { // 情况一st.push(i);}if (height[i] == height[st.top()]) { // 情况二st.pop(); // 其实这一句可以不加,效果是一样的,但处理相同的情况的思路却变了。st.push(i);} else { // 情况三while (!st.empty() &&height[i] > height[st.top()]) { // 注意这里是whileint mid = st.top();st.pop();if (!st.empty()) {int h = min(height[st.top()], height[i]) - height[mid];int w = i - st.top() - 1; // 注意减一,只求中间宽度sum += h * w;}}st.push(i);}}return sum;}
};

今日总结:接雨水。

http://www.lryc.cn/news/307575.html

相关文章:

  • 【web】nginx+php环境搭建-关键点(简版)
  • 1、什么是ETF?
  • 备战蓝桥杯Day18 - 双链表
  • 【大数据】Flink 内存管理(二):JobManager 内存分配(含实际计算案例)
  • (2024,Sora 逆向工程,DiT,LVM 技术综述)Sora:大视觉模型的背景、技术、局限性和机遇回顾
  • MySQL基础(二)
  • el-table 多选表格存在分页,编辑再次操作勾选会丢失原来选中的数据
  • 备战蓝桥杯————如何判断回文链表
  • linux 文本编辑命令【重点】
  • C#面:ref 和 out 的区别
  • php脚本输出中文在浏览器中显示乱码
  • 【Unity每日一记】角色控制器Character Contorller
  • Kafka入门介绍一
  • leetcode 3.反转链表;
  • 【蓝桥杯】快读|min和max值的设置|小明和完美序列|​顺子日期​|星期计算|山
  • 半小时到秒级,京东零售定时任务优化怎么做的?
  • stm32——hal库学习笔记(ADC)
  • 一周学会Django5 Python Web开发-Http请求HttpRequest请求类
  • element el-date-picker 日期组件置灰指定日期范围、禁止日期范围日期选择
  • 202434读书笔记|《繁星·春水》——残花缀在繁枝上,鸟儿飞去了,撒得落红满地,生命也是这般的一瞥么?
  • Golang 关于 interface 接口的理解
  • SQL注入漏洞解析--less-7
  • java高级——反射
  • 云计算新宠:探索Apache Doris的云原生策略
  • 【PHP设计模式08】装饰模式
  • 寒假作业Day 01
  • 学习JAVA的第四天(基础)
  • 拉美巴西阿根廷媒体宣发稿墨西哥哥伦比亚新闻营销如何助推跨境出海推广?
  • SpringMVC 学习(九)之拦截器
  • TCP/IP-常用网络协议自定义结构体