Trapping Rain Water leetcode java
题目:
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!
题解:
参考:低调小一(http://blog.csdn.net/wzy_1988/article/details/17752809)的解题思路
water的容量,取决于A[i]左右两边的高度(可延展)较小值与A[i]的差值,即volume[i] = [min(left[i],
right[i]) - A[i]] * 1,这里的1是宽度,如果the width of each bar is 2,那就要乘以2了”
那么如何求A[i]的左右高度呢? 要知道,能盛多少水主要看短板。那么对每个A[i]来说,要求一个最高的左短板,再求一个最高的右短板,这两个直接最短的板子减去A[i]原有的值就是能成多少水了。
所以需要两遍遍历,一个从左到右,找最高的左短板;一个从右到左,找最高的右短板。最后记录下盛水量的总值就是最终结果了。
代码如下:
1 public int trap(int[] A) {
2 if (A == null || A.length == 0)
3 return 0;
4
5 int i, max, total = 0;
6 int left[] = new int[A.length];
7 int right[] = new int[A.length];
8
9 // from left to right
left[0] = A[0];
max = A[0];
for (i = 1; i < A.length; i++) {
left[i] = Math.max(max, A[i]);
max = Math.max(max, A[i]);
}
// from right to left
right[A.length-1] = A[A.length-1];
max = A[A.length-1];
for (i = A.length-2; i >= 0; i--) {
right[i] = Math.max(max, A[i]);
max = Math.max(max, A[i]);
}
// trapped water (when i==0, it cannot trapped any water)
for (i = 1; i < A.length-1; i++) {
int bit = Math.min(left[i], right[i]) - A[i];
if (bit > 0)
total += bit;
}
return total;
}
对照着代码再看原来的例子:
index: 0 1 2 3 4 5 6 7 8 9 10 11
A[index]: 0 1 0 2 1 0 1 3 2 1 2 1
left[index]: 0 1 1 2 2 2 2 3 3 3 3 3
right[index]: 3 3 3 3 3 3 3 3 2 2 2 1
min[i]: 0 1 1 2 2 2 2 3 2 2 2 1
bit[i]: - 0 1 0 1 2 1 0 0 1 0 0
那么根据上表可以算出最终结果是6。
Reference:http://blog.csdn.net/wzy_1988/article/details/17752809
Trapping Rain Water leetcode java的更多相关文章
- Trapping Rain Water [LeetCode]
Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- [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] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 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 ...
随机推荐
- OSI、TCP、IP、UDP 这些都是啥??
一个大大的问号首先抛出,计算机之间是如何进行通信的? 计算机网络是通过传输介质.通信设施和网络通信协议,把分散在不同地点的计算机设备互连起来,实现资源共享和数据传输的系统. 网络协议就是数据按照一定的 ...
- [MySQL-笔记]创建高性能索引
索引,MySQL中也叫“键”,是存储引擎中用于快速找到记录的一种数据结构,具体的工作方式就像书本中的索引一样,但是具体的实现方式会有差别. 一.索引分类 B-Tree索引: 优点: MyISAM中,索 ...
- Python 序列与映射的解包操作
解包就是把序列或映射中每个元素单独提取出来,序列解包的一种简单用法就是把首个或前几个元素与后面几个元素分别提取出来,例如: first, seconde, *rest = sequence 如果seq ...
- web计时机制
前面的话 页面性能一直都是Web开发人员比较关注的领域.但在实际应用中,度量页面性能的指标,是javascript的Date对象.Web Timing API改变了这个局面,让开发人员通过javasc ...
- Spring的模块组成
Spring的模块组成 1.核心容器:核心容器提供 Spring 框架的基本功能(Spring Core).核心容器的主要组件是 BeanFactory,它是工厂模式的实现. BeanFactory ...
- Luogu 4492 [HAOI2018]苹果树 组合数
https://www.luogu.org/problemnew/show/P4492 找每个编号的点的父边的贡献,组合数和阶乘就能算了. 我考场上怎么就是没想到呢. 调了好久好久好久好久调不出来,样 ...
- git零基础【慢慢补充】
git branch dev //创建新分支 git checkout dev //切换到新分支 git add . //把当前修改加到暂存区 git commit -m "代码描 ...
- lua协程----ngx-lua线程学习笔记
--[[ - @desc lua数据输出 - @param string 字符串 - return string --]] function dump(v) if not __dump then fu ...
- 英特尔老款CPU支持虚拟化对照表(转)
说明:一般来说新款的挤牙膏公司出的CPU都基本支持虚拟化,但不包括Atom系列的,也就是小主机低功耗机器使用的CPU. Intel® Virtualization Technology List YE ...
- HTML5学习笔记4
10.表单元素表单元素用于获取用户的输入数据form 表示HTML表单属性: action 表示表单提交的页面 method 表示表单提交的请求方式:有POST和GET两种,默认GET(P ...