【问题】

给定 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. 网易云信-新增自定义消息(iOS版)

    https://www.jianshu.com/p/2bfb1c4e9f21 前言 公司业务需要,PC端,移动端都用到了第三方 网易云信 IM来实现在线客服咨询.在这当中难免遇到一些需求是网易云信没有 ...

  2. Windows XP 常用DOS命令

      winver 检查Windows版本 wmimgmt.msc 打开windows管理体系结构(WMI) wupdmgr windows更新程序 wscript windows脚本宿主设置 writ ...

  3. VBS 脚本对象

    Dictionary对象(1) 1.        属性: a)        compareMode b)       count c)        key d)       item 2.    ...

  4. 分析一次double强转float的翻车原因(转载)

    https://www.cnblogs.com/CoderAyu/p/11489577.html float只能保证7位有效数字. double d = 8345933; float f = (flo ...

  5. 论文写作+gnuplot制图

    一:论文写作 论文写作推荐使用LATEX+TEXStudio+TEXLive 1.CTeX官方网站:http://www.ctex.org/HomePage ,他类似于python环境 2.TeXst ...

  6. 解决Ubuntu(linux)系统中PHP的curl函数无法使用的问题

    我之前用的Windows的服务器,未出现问题,后来把服务器重装了系统,今天在学微信公众号获取信息的时候,发现curl函数出现了问题...... 解决方法 首先连接上服务器,找到/etc/php/7.0 ...

  7. PaperReading20200222

    CanChen ggchen@mail.ustc.edu.cn   VS-GAE Motivation: With the publication of NAS101, researchers can ...

  8. R语言 subset()函数用法

    subset() 函数: subset(dataset , subset , select ) dataset 是 要进行操作的数据集 subset 是对数据的某些字段进行操作 select 选取要显 ...

  9. redis-Hash(哈希表)

    Redis hash 是一个string类型的field和value的映射表,它的添加.删除操作都是O(1)(平均).hash特别适用于存储对象,将一个对象存储在hash类型中会占用更少的内存,并且可 ...

  10. JavaScript--选择器

    1.选择器是jQuery的根基,在jQuery中,对事件处理,遍历DOM和Ajax操作都依赖于选择器. 2.选择的优点: --写法简洁: --完善的事件处理机制. 3.基本选择器: --基本选择器是j ...