双指针算法“接雨水”

链接:https://leetcode-cn.com/problems/trapping-rain-water/

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。

  1. 输入: [0,1,0,2,1,0,1,3,2,1,2,1]
  2. 输出: 6

  1. class Solution {
  2. public:
  3. int trap(vector<int>& height) {
  4. int left = 0;
  5. int right = height.size() - 1;
  6. int left_bar_max = 0;
  7. int right_bar_max = 0;
  8. int ans = 0;
  9. //左指针、右指针重合跳出循环返回答案
  10. while(left < right)
  11. {
  12. //左边高度小于右边高度,便从左到右遍历
  13. if(height[left] < height[right])
  14. {
  15. //左指针高度大于左边最大高度,将左边最大高度更新成左指针指向的高度
  16. if(height[left] > left_bar_max)
  17. {
  18. left_bar_max = height[left];
  19. }
  20. else
  21. {
  22. ans += left_bar_max - height[left];
  23. }
  24. left++;
  25. }
  26. else
  27. {
  28. if(height[right] > right_bar_max)
  29. {
  30. right_bar_max = height[right];
  31. }
  32. else
  33. {
  34. ans += right_bar_max - height[right];
  35. }
  36. right--;
  37. }
  38. }
  39. return ans;
  40. }
  41. };

双指针解题思路:

 left :从左到右的数组下标
 right:从右到左的数组下标
 left_bar_max:左边的最大值
 right_bar_max:右边的最大值

当right_bar_max>left_bar_max,积水高度将由 left_bar_max 决定,类似地 left_bar_max>right_bar_max 积水的高度将由 right_bar_max 决定。
所以我们可以认为如果一端有更高的条形块(例如右端),积水的高度依赖于当前方向的高度(从左到右)。当我们发现另一侧(右侧)的条形块高度不是最高的,我们则开始从相反的方向遍历(从右到左)。
我们必须在遍历时维护left_bar_max 和right_bar_max,但是我们现在可以使用两个指针交替进行,实现 1 次遍历即可完成

  1. right_bar_max
    left_bar_max  __
  2. __ | |
  3. | |__ __?????????????????????? | |
  4. __| |__| __| |__
  5. left right

C++ leetcode接雨水的更多相关文章

  1. LeetCode.接雨水

    题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完.我自己强行给加了这句: && m ...

  2. 【完虐算法】LeetCode 接雨水问题,全复盘

    大家好! 动态规划题目是总结的比较完整了.下面是自从和大家刷开题总结的动态规划解题方法. 今年全国夏天雨是真的多,突然想到今年北京的夏天也不像往年那么热.不知不觉就稳稳地度过了夏天来到秋天. 恰巧前几 ...

  3. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  4. 腾讯,华为,阿里…7家Java后端面试经验大公开!

    感觉面试还是主要围绕简历来问的,所以不熟悉的东西最好不要随便写上去.项目和基础都很重要,整体的基础知识的框架可以参考GitHub 上 CYC2018的博客,分类很全,但是深入的学习还是要自己去看书,写 ...

  5. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  8. LeetCode:接雨水【42】

    LeetCode:接雨水[42] 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1, ...

  9. [LeetCode]42. 接雨水(双指针,DP)

    题目 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下, ...

随机推荐

  1. Java基础00-Stream流34

    1. Stream流 Stream流 1.1 体验Stream流 代码示例: //需求:按照下面的要求完成集合的创建和遍历 public class StreamDemo { public stati ...

  2. SpringBoot配置Https

    HTTPS (全称:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认 ...

  3. C语言变量为何先定义后使用

    C语言中,对变量的使用,首先要先定义.说明其数据类型.原因可能如下: 1不同类型的变量,其编码表示方式可能不同. 2不同类型的变量,其占有的空间大小不同.不事先说明无法在内存中开辟空间.

  4. MYSQL时间戳和日期相互转换 笔记整理

    相关函数: date_format(date, format) 函数,MySQL日期格式化函数date_format() unix_timestamp() 函数 str_to_date(str, fo ...

  5. python读取数据写入excel的四种操作

    Python对Excel的读写主要有:xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种 xlutils结合xlrd: 操作的是以xls后缀的excel,读取文件保留原格式 ...

  6. 忘记oracle的sys和system的密码

    step1:通过cmd打开命令提示符, sqlplus /nolog step2:输入conn /as sysdba step3:输入alter user system identified by 新 ...

  7. Fast Run:提高 MegEngine 模型推理性能的神奇功能

    作者:王博文 | 旷视 MegEngine 架构师 一.背景 对于深度学习框架来说,网络的训练/推理时间是用户非常看中的.在实际生产条件下,用户设计的 NN 网络是千差万别,即使是同一类数学计算,参数 ...

  8. Mysql命令语句

    常用的管理命令 SHOW DATABASES;    //显示当前服务器下所有的数据库 USE 数据库名称;          //进入指定的数据 show tables;              ...

  9. Maven项目思考&实战

    参考了网络上很多文章, 特此感谢. Maven项目规范 同一项目中所有模块版本保持一致 子模块统一继承父模块的版本 统一在顶层模块Pom的节中定义所有子模块的依赖版本号,子模块中添加依赖时不要添加版本 ...

  10. laod

    https://iiio.io/download/20170120/ https://laod.cn/hosts/2017-google-hosts.html Copyright (c) 老D博客:h ...