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 ...
随机推荐
- git 设置bitbucket 邮箱、用户
1. git config --global user.name "youname" 2 .git config --global user.email "youeami ...
- 理解 Python 中的元类
本文编程环境:Jupyter NoteBook python3 类也是对象 在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在 Python 中这一点仍然成立: class Objec ...
- NetCore+Dapper WebApi架构搭建(六):添加JWT认证
WebApi必须保证安全,现在来添加JWT认证 1.打开appsettings.json添加JWT认证的配置信息 2.在项目根目录下新建一个Models文件夹,添加一个JwtSettings.cs的实 ...
- DataGridView、List<T>相关操作
一.DataGridView数据转成DataTable 1.已绑定过数据源:DataTable dt = (dataGridView1.DataSource as DataTable) 2.未绑定过数 ...
- Failed to resolve: com.android.support:design:25.4.0
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 错误:(27, 13) Failed to resolve: com.android.s ...
- SDOI 2017 Round2 滚粗了
没进省队qwq 技不如人,甘拜下风
- luoguP2490 [SDOI2011]黑白棋 博弈论 + 动态规划
博弈部分是自己想出来的,\(dp\)的部分最后出了点差错QAQ 从简单的情况入手 比如\(k = 2\) 如果有这样的局面:$\circ \bullet $,那么先手必输,因为不论先手怎样移动,对手都 ...
- hdu 4025 Equation of XOR 状态压缩
思路: 设: 方程为 1*x1 ^ 1*x2 ^ 0*x3 = 0; 0*x1 ^ 1*x2 ^ 1*x3 = 0; 1*x1 ^ 0*x2 ^ 0*x3 = 0 把每一列压缩成一个64位整数,因为x ...
- windows下配置ssh(FreeSSHD + putty)
windows下配置ssh(FreeSSHD + putty): 1.关于配置过程找到一篇很好的博客,推荐大家先好好看一下,这篇博文解决了大方向问题. 地址:http://blog.csdn.net/ ...
- BZOJ 3876: [Ahoi2014]支线剧情 带下界的费用流
3876: [Ahoi2014]支线剧情 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3876 Description [故事背景] 宅 ...