Trapping Raining Water 解答
Question
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
.
Solution
Key to the solution is to know that for each point a[i], the max are is calculated by:
min(left,right) – a[i]
left is the maximum height before a[i], right is the maximum height after a[i].
Therefore, we can create two arrays to record left most height and right most height for each point. Time complexity O(n).
public class Solution {
public int trap(int[] height) {
if (height == null || height.length < 1)
return 0;
int length = height.length;
int[] leftMost = new int[length];
int[] rightMost = new int[length];
// First, find left biggest hight
leftMost[0] = 0;
for (int i = 1; i < length; i++)
leftMost[i] = Math.max(leftMost[i - 1], height[i - 1]); // Then, find right biggest hight
rightMost[length - 1] = 0;
for (int i = length - 2; i >= 0; i--)
rightMost[i] = Math.max(rightMost[i + 1], height[i + 1]); // Calculate sum
int result = 0;
for (int i = 0; i < length; i++) {
int tmp = Math.min(leftMost[i], rightMost[i]) - height[i];
if (tmp > 0)
result += tmp;
}
return result;
}
}
Trapping Raining Water 解答的更多相关文章
- [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] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [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 ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
随机推荐
- mysql5.5 无法创建实例,error 16001
今天想用jdbc做个小程序,结果发现好久不用的mysql不好用了,我装的是社区版(win7)环境下,按理说不可能出问题,找了一堆解决方案都没解决,准备重装的时候想把mysql服务停了,直接在dos输入 ...
- UVA10304---(区间DP)
第一开始想着枚举根节点,然后记忆化搜索..结果TLE,最后还是看了一眼题解瞬间明白了..唉,还是思维太局限了 由于数据是按照从小到大排列的,可以自然地组成一颗二叉排序树. 设dp[i][j]是区间[i ...
- VMdomainXml
1,One,Euc,Ostack 虚拟磁盘镜像制作方法[Windows,Linux,类linux OS](1,基于ios部署系统生成img,2基于vm xml定义部署系统生成img qcow2) 如需 ...
- STL List容器
转载http://www.cnblogs.com/fangyukuan/archive/2010/09/21/1832364.html 各个容器有很多的相似性.先学好一个,其它的就好办了.先从基础开始 ...
- JUnit三分钟教程 ---- 实际应用
JUnit三分钟教程 ---- 实际应用 摘自http://lavasoft.blog.51cto.com/62575/65775 接上文"JUnit三分钟教程 ---- 快速起步&qu ...
- 游标的使用实例(Sqlserver版本)
游标,如果是之前给我说这个概念,我的脑子有二个想法:1.你牛:2.我不会 不会不是理由,更不是借口,于是便要学习,本人属性喜欢看代码,不喜欢看书的人,所以嘛,文字对我没有吸引力:闲话少说啊,给大家提供 ...
- vim 的配色方案
浅色: http://www.vimninjas.com/2012/09/14/10-light-colors/ 深色: http://www.vimninjas.com/2012/08/26/10- ...
- NoSQL 简介
NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 在现代的计算系统上每天网络上都会产生庞大的数据量. 这些数据有很大一部分是由关系数据库管理系统( ...
- hdu 4908 BestCoder Sequence
# include <stdio.h> # include <algorithm> using namespace std; int main() { int n,m,i,su ...
- 一些基础的.net用法
一.using 用法 using 别名设置 using 别名 = System.web 当两个不同的namespace里有同名的class时.可以用 using aclass = namespace1 ...