【LeetCode】接雨水
【问题】
给定 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】接雨水的更多相关文章
- LeetCode.接雨水
题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完.我自己强行给加了这句: && m ...
- 【完虐算法】LeetCode 接雨水问题,全复盘
大家好! 动态规划题目是总结的比较完整了.下面是自从和大家刷开题总结的动态规划解题方法. 今年全国夏天雨是真的多,突然想到今年北京的夏天也不像往年那么热.不知不觉就稳稳地度过了夏天来到秋天. 恰巧前几 ...
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- C++ leetcode接雨水
双指针算法"接雨水" 链接:https://leetcode-cn.com/problems/trapping-rain-water/ 给定 n 个非负整数表示每个宽度为 1 的柱 ...
- 腾讯,华为,阿里…7家Java后端面试经验大公开!
感觉面试还是主要围绕简历来问的,所以不熟悉的东西最好不要随便写上去.项目和基础都很重要,整体的基础知识的框架可以参考GitHub 上 CYC2018的博客,分类很全,但是深入的学习还是要自己去看书,写 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- LeetCode:接雨水【42】
LeetCode:接雨水[42] 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1, ...
- [LeetCode]42. 接雨水(双指针,DP)
题目 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下, ...
随机推荐
- A way to find out how activity for mssql and oracle
Dear buddy, Have you confuse that how activity about my databases, if we conside it by using backup ...
- JavaScript一个简单的图片切换布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Python 基础之模块之math random time
一:math 数学模块import math#(1)ceil() 向上取整操作 (对比内置round)res = math.ceil(6.001) #注意精度损耗print(res)#(2)floo ...
- layer弹出框包含页面
参考:http://www.cnblogs.com/zhengchenhui/p/6038865.html
- 虚拟机下安装 VMwareTools 实现宿主机和虚拟机的文件共享
$ mount /dev/sr0 /media/ #点击 虚拟机 安装 VMwareTools 挂载 $ cd /media/ $ cp VMwareTools-10.1.6-5214329.tar. ...
- 第1节 storm编程:3、storm的架构模型的介绍
nimbus:主节点,接收客户端提交的任务,并且分配任务,新的版本当中nimbus已经可以有多个了 zookeeper集群:storm依靠zk来保存一些节点信息,nimbus将分配的任务信息都写入到z ...
- JS链接转换为二维码
这里用到一个JQ插件 qrcode.js 下载地址https://github.com/jeromeetienne/jquery-qrcode 先引入 <script src="j ...
- C++ 一篇搞懂继承的常见特性
微信公众号:「小林coding」 用简洁的方式,分享编程小知识. 继承和派生 01 继承和派生的概念 继承: 在定义一个新的类 B 时,如果该类与某个已有的类 A 相似(指的是 B 拥有 A 的全部特 ...
- Tomcat+JSP经典配置实例
经常看到jsp的初学者问tomcat下如何配置jsp.servlet和bean的问题,于是总结了一下如何tomcat下配置jsp.servlet和ben,希望对那些初学者有所帮助. 一.开发环境配置 ...
- java表单基础
一.表单 基本语法: <form method="表单提交方式(post/get)" action="表单提交地址"> </ ...