LeetCode OJ 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.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
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!
【题目分析】
题目给出一个整数序列代表高度图,要求计算下雨后图中的积水量。
【思路】
我们只要找到高度图中的凹陷部分,然后把所有凹陷的容积加起来即可。那么如何找到这些凹陷部分呢?
有这样一个想法:给定高度图两端的高度,那些比这两端都低的部分肯定会形成凹陷,如果是相等则不形成凹陷,如果比当前高度高,则我们要重新确定高度图的两端高度。这个过程如下:
1. 初始两端最低高度high = 0, 容量capacity = 0;
2. height[begin] <= high,不形成凹陷,begin++;
3. height[begin] > high; height[end] > high;high = min(height[begin], height[end]); 因此high = 1;
此时height[begin] == high,height[end] == high;所以begin++,end--;
4. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity = 1;begin++;
5. height[begin] > high; height[end] > high;high = min(height[begin], height[end]); 因此high = 2;
此时height[begin] == high,height[end] == high;所以begin++,end--;
6. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity = 2;begin++;
height[end] < high;形成凹陷,capacity += high - height[begin];此时capacity = 3;end--;
7. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity = 5;begin++;
height[end] = high;不形成凹陷;end--;
7. height[begin] < high;形成凹陷,capacity += high - height[begin];此时capacity = 6;begin++;
8. 返回最大容量6;
【java代码】
public class Solution {
public int trap(int[] height) {
if(height == null || height.length <= 2) return 0; int sum = 0, maxhigh = 0;
int begin = 0, end = height.length-1;
while(begin <= end){
if(height[begin] < maxhigh){
sum += maxhigh - height[begin++];
}
else if(height[end] < maxhigh){
sum += maxhigh - height[end--];
}
else{
maxhigh = Math.min(height[begin], height[end]);
if(height[begin] <= maxhigh) begin++;
if(height[end] <= maxhigh) end--;
}
}
return sum;
}
}
LeetCode OJ 42. Trapping Rain Water的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- leetcode problem 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
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- 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 ------------------------------------------------------------- ...
随机推荐
- DuiLib 中滚动条不显示的问题
DuiLib 很好用,同时在没有完全理解源码的前提下,坑也不少,比如今天遇到的添加滚动条不显示... 情况是这样的,将一个页面作为Tab控件的其中一页,为了代码不窝在一起,就没有在CreateCont ...
- python3中字典的copy
字典是可变的: first和second同时指向一个字典.first修改也会影响second.在程序中一定注意对字典参数的修改会对原始的字典进行修改.这也体现了字典是可变的. 字典的copy方法是浅拷 ...
- layoutSubviews在什么情况下会被调用
layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews. 2.addSubview会触发layoutSubviews. 3.设置view的Frame ...
- moodle其他搜集
1.将面包屑的符号换成">>",找到皮肤包里的config.php文件,在最后加入 $THEME->rarrow=">>"; ...
- RSA算法记录----摘抄
RSA算法原理(一) "公钥加密算法". 因为它是计算机通信安全的基石,保证了加密数据不会被破解.你可以想象一下,信用卡交易被破解的后果. 进入正题之前,我先简单介绍一下,什么 ...
- 无线hacking系统—wifislax
简介 官方中文网站: http://wifislax.cn/ WiFiSlax 是在Slax基础上定制出来的,由西班牙开发.它包含了各种各样的安全和诊断工具.该发行主要的成名原因是把各种各样的非官方网 ...
- eclipse中link方式安装插件
今天需要给eclipse安装svn插件,觉得link方式便于管理于是就打算用这种方式来安装. 我电脑上的eclipse的安装目录是 E:\tools\eclipse 下面开始安装 1.在ecl ...
- dev Gridcontrol根据其cell里面的值显示不同颜色
要改变cell值得颜色 需要用到cellTemplate和convert <DataTemplate x:Key="PercentageCellColorTemplate"& ...
- [Q]pdfFactory虚拟打印机的安装
安装打图精灵过程中会提示是否安装pdfFactory虚拟打印机,建议选择安装. 若未安装,在安装打图精灵之后想安装pdfFactory,该软件可以在打图精灵应用程序文件夹下找到( 系统"开始 ...
- [Q]自定义纸张大小
问:当打印机纸张列表里没有符合要求的纸张大小,例如如何打印加长图?答:当打印非标准图框时,你可能在图纸列表里找不到想要纸幅.你需要自己新建你需要的纸幅,以pdfFactory虚拟打印机为例(其它打印机 ...