[leetcode]42. Trapping Rain Water雨水积水问题
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
题意:
给定一个地形,计算能存多少雨水。
思路:
1. 扫数组,找到最高柱子并以此为中心,将数组分为两半。
2. 对左半部分而言,可以想象右边一定有堵墙,只需要每次更新leftMost就能决定water大小
3. 对右半部分而言,同理。
代码:
class Solution {
public int trap(int[] height) {
// find hightest bar
int peak_index = 0;
for(int i = 0; i < height.length; i++){
if(height[i] > height[peak_index]){
peak_index = i;
}
}
// 1. from left to peak_index
int leftMostBar = 0;
int water = 0;
for(int i = 0; i < peak_index; i++){
if (height[i] > leftMostBar){
leftMostBar = height[i];
}else{
water = water + leftMostBar - height[i];
}
} // 2. from right to peak_index
int rightMostBar = 0;
for(int i = height.length - 1; i > peak_index; i--){
if (height[i] > rightMostBar){
rightMostBar = height[i];
}else{
water = water + rightMostBar - height[i];
}
}
return water;
}
}
[leetcode]42. Trapping Rain Water雨水积水问题的更多相关文章
- LeetCode 42 Trapping Rain Water(积水体积)
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意 ...
- [LeetCode]42. Trapping Rain Water雨水填坑
这个题难点在于无法保证右边是不是有更高的墙可以保证挡住水 双指针可以解决 /* 两边指针保证,保证另外一边肯定有能挡住水的地方. 如果从一边开始,不考虑另一边,是无法保证右边肯定有挡水的墙,如果右边只 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [LeetCode] 42. 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(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- [LeetCode] 42. Trapping Rain Water 解题思路
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
随机推荐
- IO流小笔记
File file=new File ();括号里面写路径 exists()判断文件是否存在:isfile()是判断已经存在的文件是文件还是目录: mkdir()和createNewFile()区别在 ...
- PythonStudy——Python 内置函数 Built-in function
内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...
- c# automapper 使用(一)
一.最简单的用法 有两个类User和UserDto public class User { public int Id { get; set; } public string Name { get; ...
- kvm报错集
虚拟机console窗口看到一些报错 也可以在终端使用dmesg命令查看 [17617.701174] kvm [17393]: vcpu0 unhandled rdmsr: 0x1ad [19053 ...
- “数据上帝” Jeff Hammerbacher
出生于1983年的数学天才Jeff Hammerbacher在23岁时加入了Facebook,一手组建起数据分析队伍.他是“数据科学”(data science)一词的提出者之一,被人们称为“数据上帝 ...
- [转]马上2018年了,该不该下定决心转型AI呢
转自:http://blog.csdn.net/eNohtZvQiJxo00aTz3y8/article/details/78941013 2017年,AI再一次迈向风口,但我们如何判断未来走向?应不 ...
- 【java】模板方法设计模式
模板方法:在定义功能时,功能一部分是确认的,另一部分是不确认的或者后续会变化的.这时可以把不确定的部分暴露出去,定义成抽象类或者接口,由子类来完成. abstract class GetDuring ...
- 【OpenStack】相关概念
网络 network和subnet Service subnets: 创建network,subnet, instances 官方示例 Network components: Switches/ Ro ...
- ubuntu16.04 安装最新版nodejs
ubuntu软件仓库中自带的nodejs版本过低 $ apt-cache policy nodejs nodejs: Installed: (none) Candidate: 4.2.6~dfsg-1 ...
- EL(Expression Language)和JSTL标签(JSP Standard Tag Library)
一.EL表达式: Expression Language提供了在 JSP 脚本编制元素范围外(例如:脚本标签)使用运行时表达式的功能.脚本编制元素是指页面中能够用于在JSP 文件中嵌入 Java代码的 ...