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 ...
随机推荐
- In 和Exists
1.exist,not exist一般都是与子查询一起使用. In可以与子查询一起使用,也可以直接in (a,b.....) 2.exist会针对子查询的表使用索引. not exist会对主子查询都 ...
- Here is a 10-line template that can solve most 'substring' problems子字符串问题的模板
转载自leetcode评论区:https://discuss.leetcode.com/topic/30941/here-is-a-10-line-template-that-can-solve-mo ...
- [POI2013]Bajtokomputer
[POI2013]Bajtokomputer 题目大意: 给定一个长度为\(n(n\le10^6)\)的由\(\{-1,0,1\}\)组成的序列,你可以进行\(A_i+=A_{i-1}\)这样的操作, ...
- Splay-Tree理解
简介 splay tree其实就是不停的旋转,没进行一个操作都要进行旋转:例如,当访问某一个结点的时候,会通过旋转其结点使得该结点变为树根,这样保证其的平均复杂度为O(nlogn); 其的操作包括: ...
- lor框架代码分析
属性 lor: version router route request response fn app create_app Router Route Request Response 属性 lor ...
- BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4902 Solved: 1879 题目连接 http:/ ...
- 记ie8及以下版本ie的flash的addCallback的一坑
近来有一需求,播放声音,我在高端浏览器实现了html5 audio标签.低端浏览器实现了flash兼容.但是在调试ie8以下的浏览器发现js死活调不了flash里的addCallback的方法,总报错 ...
- javascript小记-作用域
一.全局作用域 全局作用域的变量不论在什么时候都可以直接引用,而不必通过全局对象:满足以下条件的变量属于全局作用域:1.在最外层定义的变量2.全局对象的属性3.任何地方隐式定义的变量(未定义直接赋值的 ...
- [Dynamic Language] Python3.7 源码安装 ModuleNotFoundError: No module named '_ctypes' 解决记录
Python3.7 源码安装 ModuleNotFoundError: No module named '_ctypes' 解决记录 源码安装时报错 File "/home/abeenser ...
- nyist oj 214 单调递增子序列(二) (动态规划经典)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 ,a2...,an}(0<n<=100000).找出单调递增最长子序列,并求出其长度 ...