LeetCode[42] 接雨水

news/2025/3/20 0:54:32/

动态规划

  1. left_max[i] = height[i]左侧的最高高度
  2. right_max[i] = height[i]右侧的最高高度
  3. height[i]能接多少水?min(left_max[i], right_max[i])-height[i]
class Solution {
public:int trap(vector<int>& height) {int len = height.size();vector<int> left_max(len,0);  // 左侧最高高度vector<int> right_max(len,0); // 右侧最高高度// 遍历获取左侧最高高度left_max[0] = height[0];for(int i=1;i<len;i++){left_max[i] = max(left_max[i-1],height[i]);}// 遍历获取右侧最高高度right_max[len-1] = height[len-1];for(int i=len-2;i>=0;i--){right_max[i] = max(right_max[i+1],height[i]);}// 获取接水量int res = 0;for(int i=0;i<len;i++){res += (min(left_max[i], right_max[i]) - height[i]);}return res;}
};
  1. 优化:左侧最高高度可以只使用一个int变量,遍历获取接水量时动态更新
class Solution {
public:int trap(vector<int>& height) {int len = height.size();int left_max = 0;vector<int> right_max(len,0);right_max[len-1] = height[len-1];for(int i=len-2;i>=0;i--){right_max[i] = max(right_max[i+1],height[i]);}int res = 0;for(int i=0;i<len;i++){left_max = max(left_max, height[i]);res += (min(left_max, right_max[i]) - height[i]);}return res;}
};

双指针

  1. 动态规划优化后依然需要维护左侧或者右侧的最高值,空间开销为O(n)
  2. 使用左右指针动态维护左侧和右侧最高值
class Solution {
public:int trap(vector<int>& height) {int res = 0;int left = 0;int right = height.size()-1;int left_max = 0;int right_max = 0;while(left<=right){// 当前位置接的水 =(先前最高值 - 当前位置的高度)// 当左指针的最高高度大于等于右指针最高高度,计算右指针的接水量// 当右指针的最高高度大于左指针最高高度,计算左指针的接水量// 原因:另一侧比当前一侧高,所以当前位置条件满足时肯定能接到水if(left_max<=right_max){left_max = max(left_max,height[left]);res = res + left_max - height[left++];}else{right_max = max(right_max,height[right]);res = res + right_max - height[right--];}}return res;}
};

  1. 从左到右遍历数组,遍历到下标 i 时
  2. 如果栈内至少有两个元素,记栈顶元素为 top
  3. top 的下面一个元素是 left,则一定有 height[left]≥height[top]
  4. 如果 height[i]>height[top],则得到一个可以接雨水的区域
    • 该区域的宽度是 i−left−1
    • 高度是 min(height[left],height[i])−height[top]
    • 根据宽度和高度即可计算得到该区域能接的雨水量
  5. 在对 top 计算能接的雨水量之后,left 变成新的 top,重复上述操作
  6. 直到栈变为空,或者栈顶下标对应的 height 中的元素大于或等于 height[i]
class Solution {
public:int trap(vector<int>& height) {stack<int> mystack;int res = 0;for(int i=0; i<height.size(); i++){while (!mystack.empty() && height[i] > height[mystack.top()]) {int top = mystack.top();mystack.pop();if(mystack.empty()) break; // 例如第一个高度是0,没有左侧边界,是接不到水的int left = mystack.top();int curr_width = i - left - 1;int curr_height = min(height[left], height[i]) - height[top];res += curr_height*curr_width;}mystack.push(i);}return res;}
};

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

相关文章

Unity导出WebGL

在Build Settings页面中平台&#xff08;Platform&#xff09;切换到WebGL平台 如何没有安装WebGL扩展插件&#xff0c;点击下载&#xff08;Open Download Page&#xff09; 下载扩展安装文件WebGL-Support-for-Editor-2023.1.0f1c1.exe 下载地址&#xff1a; http://downlo…

Docker 构建 nginx-redis-alpine 项目详解

Docker 构建 nginx-redis-alpine 项目详解 一、课程概述 嘿&#xff0c;朋友们&#xff01;今天咱们要深入探索一个超级实用的项目 ——nginx-redis-alpine&#xff01;这个项目可不简单&#xff0c;它包含了好多重要的知识点&#xff0c;像文件目录结构、核心文件的作用及配…

游戏成瘾与学习动力激发策略研究——自我效能理论

自我效能理论(Self-Efficacy Theory)由著名心理学家阿尔伯特班杜拉(Albert Bandura)于1977年提出,是解释个体对自己能否成功完成特定任务的核心信念如何影响行为选择、努力程度和抗压能力的重要理论。它不仅是心理学领域的基石理论,更为你描述的“游戏成瘾后自我怀疑”现…

Linux 文件管理、传输与系统调优指南

1. tar 实用程序的作用与使用 作用&#xff1a; tar&#xff08;Tape Archive&#xff09;是一个用于将多个文件或目录打包成单一文件的工具。它本身不压缩文件&#xff0c;但可以与其他压缩工具结合使用&#xff0c;生成压缩归档文件。 核心功能&#xff1a; 归档&#xff1…

Springboot中的@ConditionalOnBean注解:使用指南与最佳实践

在使用Spring Boot进行开发时&#xff0c;大家应该都听说过条件注解&#xff08;Conditional Annotations&#xff09;。其中的ConditionalOnBean注解就很有趣&#xff0c;它帮助开发者在特定条件下创建和注入Bean&#xff0c;让你的应用更加灵活。今天就来聊聊这个注解的使用场…

svn-1.7.22安装

下载svn包&#xff1a; Index of /dist/subversion 编译&#xff1a; 安装依赖库&#xff1a;yum install sqlite sqlite-devel 否则编译的时候不通过&#xff1a;报错&#xff1a;configure&#xff1a;error &#xff1a;subversion requires SQLite #cd subversion-1.7…

Python学习记录 20250318

函数进阶 1.作用域&#xff1a;全局变量、局部变量 全局变量 局部变量 关键字global可以将 局部变量声明为全局变量 2.匿名函数 匿名函数相较于普通函数我认为只是减少了代码量&#xff0c;关键词 lambda lambda结合if判断&#xff0c;其实用到了三目运算符&#xff08;代码…

IOS兼容 - uniapp ios固定定位失效与刘海屏的坑

问题描述 uniapp 一套代码&#xff0c;打包之后安卓可以正常显示版本号&#xff0c;IOS不可以 错误现象&#xff1a;IOS只有滚动到屏幕底部才能看到版本号 原因分析&#xff1a; IOS设计更希望屏幕跟随着用户滚动而滚动&#xff0c;所以无法实现相对浏览器窗口的固定定位。 I…