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 ------------------------------------------------------------- ...
随机推荐
- 冰刃IceSword中文版 V1.22 绿色汉化修正版
软件名称: 冰刃IceSword中文版 V1.22 绿色汉化修正版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 2.1MB 图片预览: 软件简介: Ic ...
- Html5浏览器端less应用
之前的一个布局是用rem来做的 我上一段代码 div { margin: 0.833333333rem 0; } /* 去处a标签的下划线*/ a { text-decoration: none; } ...
- 解析Properties文件
/** * 传入需要解析的文件属性,传入文件的路径 * @param para 需要获取的属性名称.也就是键值对中的键名称 * @param filepath * @return */ public ...
- Python学习笔记——基础篇【第七周】———进程、线程、协程篇(socket基础)
http://www.cnblogs.com/wupeiqi/articles/5040823.htmlhttp://i.cnblogs.com/EditPosts.aspx?postid=55437 ...
- [SQL基础教程] 2-3 逻辑运算符
[SQL基础教程] 2-3 逻辑运算符 NOT AND OR 优先级 ( )改变优先级 AND 优先级高于 OR NULL 引入三值逻辑
- Java的“影子克隆”和“深度克隆”
今天来学习学习java对象的克隆,在写代码的时候,有时候我们会这样写:对象1=对象2,也就是把对象2赋值给对象1了,但是这样做有个问题,就是如果我们修改了对象2的属性值,对象1的相同属性值也被修改了, ...
- web.xml Attribute "xmlns" was already specified for element "web-app"
报错信息:Attribute "xmlns" was already specified for element "web-app" 由于项目的重命名,出现了x ...
- echarts x轴或y轴文本字体颜色改变
1:x轴文本字体颜色改变 xAxis : [ { type : 'category', data : ['<30','30-','40-','50-','60-','>=70'], axi ...
- react使用map生成的元素,key的设定不对导致每次删除都删除最后一个
假设 你的key设置为map中的索引,假设为0,1,2(原dom树),现在你用splice删除掉1,重新渲染时,还是会按map索引按顺序渲染为0,1(新dom树),由于react渲染机制是比较的key ...
- 实战荟萃-UI篇
一. 前言 平时在处理问题的时候,经常会遇到一些奇奇怪怪的问题,今天在这里将其记录下来.这里将会列举几个常用的UI问题进行讲解 二. 导航栏 iOS导航栏绝对是个巨坑.和很多朋友聊天都是自己实现了一套 ...