【问题】

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

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

示例:

输入: [,,,,,,,,,,,]
输出:

【思路】首先我们定义left[i]表示i左边的最大值,right[i]表示i右边的最大值,并且两个数组初始化均为零。接着我们遍历整个数组,对于每一个位置i都获取其左右两边的最大值,并比较获取最小的数为level,也就是说没有柱子的话,雨水的高度为level.
从而实际对于i来说收集的雨水为level-height[i] !!!

class Solution {
public:
int trap(vector<int>& height) {
int n = height.size();
// left[i]表示i左边的最大值,right[i]表示i右边的最大值
vector<int> left(n), right(n);
for (int i = ; i < n; i++) {
left[i] = max(left[i - ], height[i - ]);
}
for (int i = n - ; i >= ; i--) {
right[i] = max(right[i + ], height[i + ]);
}
int water = ;
for (int i = ; i < n; i++) {
int level = min(left[i], right[i]);
water += max(, level - height[i]);
}
return water;
}
};

【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. C++ leetcode接雨水

    双指针算法"接雨水" 链接:https://leetcode-cn.com/problems/trapping-rain-water/ 给定 n 个非负整数表示每个宽度为 1 的柱 ...

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

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

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

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

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

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

  8. [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 ...

  9. LeetCode:接雨水【42】

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

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

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

随机推荐

  1. react - get或set 取值函数

    取值函数(getter)和存值函数(setter) 您可以添加以get或set为前缀的方法来创建getter和setter,它们是根据您正在执行的操作执行的两个不同的代码:访问变量或修改其值.对某个属 ...

  2. 浏览器输入URL后HTTP请求返回的完整过程

    图:

  3. WEB, Flask - Session&Cookie

    参考: https://blog.csdn.net/nunchakushuang/article/details/74652877 http://portal.xiaoxiangzi.com/Prog ...

  4. OC中四种遍历方式

    标准的C语言for循环.Objective-C 1.0出现的NSEnumerator.Objective-C 1.0出现的for in快速遍历.块遍历. 遍历的话,一般是NSArray.NSDicti ...

  5. idea2019 3.3最新版本破解安装教程

    直接给上神秘地址得了:(应该都可以破解) https://www.jianshu.com/p/c7bdc5819d31

  6. [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired

    Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...

  7. Keepalived——HA

    一.HA集群中的相关术语 1.节点(node) 运行HA进程的一个独立主机,称为节点,节点是HA的核心组成部分,每个节点上运行着操作系统和高可用软件服务,在高可用集群中,节点有主次之分,分别称之为主节 ...

  8. Day3-A-Problem H. Monster Hunter HDU6326

    Little Q is fighting against scary monsters in the game ``Monster Hunter''. The battlefield consists ...

  9. Python 网络编程之网络协议(未完待续)

    一:网络编程从两大架构开始 1.网络开发的两大架构 c/s 架构 : client  server B/S 架构 : Brower  server (1)bs 和 cs 架构之间的关系? (2)哪一种 ...

  10. js默认字符处理

    utf-16 每个字符是2个字节 超过2字节,就是4个字节表示