[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 ...
随机推荐
- tcpdump抓包常用参数和用法
tcpdump 与wireshark Wireshark(以前是ethereal)是Windows下非常简单易用的抓包工具.但在Linux下很难找到一个好用的图形化抓包工具.还好有Tcpdump.我们 ...
- 【java】之cron表达式
秒(~) 分钟(~) 小时(~) 天(月)(~,但是你需要考虑你月的天数) 月(~) 天(星期)(~ =SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT) .年份(-) 其中每个元素 ...
- Android Dialog.dismiss()与Activity.finish()顺序
activity.finish() 和dialog.show() 同时调用的时候, 需要先调用dialog.dismiss() 后activity.finish() 如果先直接finish()后,再触 ...
- IDEA修改git账号及密码的方法 ----绝壁好使
就这个这个位置,之前的账号信息就保存在这里,直接编辑修改,修改成你现在的账号, idea提交的时候就能直接提交,或者提交的时候要输入账号密码,ok,我的微信 18550918581,不懂再找我
- 知识点:Mysql 基本用法之函数
函数 MySQL中提供了许多内置函数 例如: sql 内置函数: 一.数学函数 ROUND(x,y) 返回参数x的四舍五入的有y位小数的值 RAND() 返回0到1内的随机值,可以通过提供一个参数(种 ...
- Java笔试面试题整理第二波
转载至:http://blog.csdn.net/shakespeare001/article/details/51200163 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- 用GDB调试程序(七)
改变程序的执行——————— 一旦使用GDB挂上被调试程序,当程序运行起来后,你可以根据自己的调试思路来动态地在GDB中更改当前被调试程序的运行线路或是其变量的值,这个强大的功能能够让你更好的调试你的 ...
- Java选择排序,插入排序,快速排序
public class Test { public static void main(String[] args) { int a[] = { 1, 2, 3, 4, 5 }; 选择排序(a); ...
- js的快速搜索
公司这几天项目很紧张,然后一直有各种乱七八糟的事,突然说要整个搜索的功能,第一时间想到的就是用php的模糊搜索,但是性能耗的很大,并且调取出的数据的速度贼慢,在百度上找到一个通过js来搜索的功能分享给 ...
- suricata 配置文件threshold
threshold threshold(阈值)关键字可用于控制规则的警报频率,它有3种模式: threshold: type <threshold|limit|both>, track & ...