lintcode-medium-Trapping Rain Water

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.

Example

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

Challenge

O(n) time and O(1) memory

O(n) time and O(n) memory is also acceptable.

public class Solution {
    /**
     * @param heights: an array of integers
     * @return: a integer
     */
    public int trapRainWater(int[] heights) {
        // write your code here

        if(heights == null || heights.length == 0)
            return 0;

        int[] left = new int[heights.length];
        int[] right = new int[heights.length];
        int res = 0;

        left[0] = 0;
        right[heights.length - 1] = 0;

        for(int i = 1; i < heights.length; i++)
            left[i] = Math.max(heights[i - 1], left[i - 1]);

        for(int i = heights.length - 2; i >= 0; i--)
            right[i] = Math.max(heights[i + 1], right[i + 1]);

        for(int i = 0; i < heights.length; i++){
            int height = Math.min(left[i], right[i]);

            if(height > heights[i])
                res += height - heights[i];
        }

        return res;
    }
}
时间: 2024-11-11 17:25:01

lintcode-medium-Trapping Rain Water的相关文章

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

[poj] The Wedding Juicer | [lintcode] Trapping Rain Water II

问题描述 给定一个二维矩阵,每个元素都有一个正整数值,表示高度.这样构成了一个二维的.有高度的物体.请问该矩阵可以盛放多少水? 相关题目:POJ The Wedding Juicer Description Farmer John's cows have taken a side job designing interesting punch-bowl designs. The designs are created as follows: * A flat board of size W cm

有意思的数学题:Trapping Rain Water

LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的部分 假设:积木宽度均为1 输入:各个积木的高度 输出:所有积木能容纳水的“面积” 思考过程 1. 逐一求积木的间隔似乎不太容易.特别对于图中3-7积木间的容积,如果可以先求底部(4-6间)的容积,当求解上层(3-7)的容积时,还需要做额外的处理,如减掉底部的高度. 2. 既然如此,可否先求出3-7

[LeetCode][JavaScript]Trapping Rain Water

Trapping Rain Water 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

Leetcode 407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note:Both m and n are less than 110. The height of each unit cell is greater tha

Trapping Rain water 诸多细节须注意

Trapping Rain Water Total Accepted: 35650 Total Submissions: 118319My Submissions Question Solution 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.

LeetCode 042 Trapping Rain Water

题目要求:Trapping Rain Water 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 elevatio

[leetcode]Trapping Rain Water @ Python

原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: 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,

Leetcode 动态规划 Trapping Rain Water

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Trapping Rain Water Total Accepted: 14568 Total Submissions: 50810My Submissions Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much wate

【leetcode】Trapping Rain Water

Trapping Rain Water 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