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

referrence

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 解答的更多相关文章

  1. [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 ...

  2. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. [LintCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  4. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  5. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  6. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  7. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  8. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  9. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

随机推荐

  1. Spoj1771-Yet Another N-Queen Problem(精确覆盖)

    Description After solving Solution to the n Queens Puzzle by constructing, LoadingTime wants to solv ...

  2. POJ3630——简单Trie树

    这个题的意思是说,给出一些字符串,判断是否有字符串是另一个字符串的前缀,当然可以用排序水过,不过这个题拿来练习一下Trie树不错. 这个题在poj的discuss上好多人说必须要静态建树,估计都是用了 ...

  3. MBI 跨国网络传销 金字塔诈骗 解密

    马来西亚  反传销博客地址http://combatingillegalpyramidscheme.blogspot.jp/search/label/Mface 需要FQ访问  闽渝警方协作抓获一名公 ...

  4. UVa 10256 The Great Divide,推断两个凸包是否相离

    先从给出的两个点集中分别计算出两个凸包, 然后推断两个凸包是否相离. #include<cstdio> #include<vector> #include<cmath&g ...

  5. Hadoop集群启动之后,datanode节点未正常启动的问题

    Hadoop集群启动之后,用JPS命令查看进程发现datanode节点上,只有TaskTracker进程.如下图所示 master的进程: 两个slave的节点进程 发现salve节点上竟然没有dat ...

  6. linux hash_map

    在linux下的hash_map hash_map本身以前本身不属于标准库,是后来引入的.有两种可能:一种可能它被放在了stdext名空间里,那么你就要使用using namespace stdext ...

  7. Linux 挂载命令 --mount

    1.挂载光盘命令  mount :  mount [-t vfstype] [-o options] device dir mount [-t 文件系统] [-o 特殊选项] 设备文件名 挂载点 -t ...

  8. 利用MetaWeblog API 自制博客发布小工具

    博客园提供了诸多数据接口, 利用这些接口可以很容易的实现博客的发布,修改,删除等 1.需要引用一个DLL:为CookComputing.XmlRpcV2 2.新建一个类,在其中是一些要实现的东西,如: ...

  9. BZOJ 2176 Strange String (最小表示法)

    题目大意: 与别的裸题的唯一不同点是其符号的ASCII码值在3 ~ 254 之间. 算法讨论: 最小表示法直接上.但是唯一不同的就是注意这里的字符范围,用char是会get wa的,所以要用unsig ...

  10. 指针参数的传递(节选 C++/C 高质量编程 林锐)

    指针参数是如何传递内存的 如果函数的参数是一个指针,不要指望用该指针去申请动态内存.示例7-4-1中,Test函数的语句GetMemory(str, 200)并没有使str获得期望的内存,str依旧是 ...