[leedcode 42] 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 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!

public class Solution {
    //动态规划,构造两个数组leftMax和rightMax,分别代表左侧最高的柱子和右侧最高的柱子
    //leftMax[i]=Math.max(leftMax[i-1],height[i-1],右侧亦然,注意leftMax[i]表示i左侧最大的柱子,不包括i
    //因此每个节点能够装水的容量可以通过Math.min(rightMax[i],leftMax[i])-height[i],求装水的总容量时,只需遍历该数组即可。
    //注意求每个节点装水的容量,可以复用之前的rightMax数组。
    //此解法时间复杂度是O(n),空间复杂度也是O(n)
    public int trap(int[] height) {
       /* int res=0;
        int len=height.length;
        if(len<=2) return res;
        int leftMax[]=new int[len];
        leftMax[0]=0;
        int rightMax[]=new int[len];
        rightMax[len-1]=0;
        for(int i=1;i<len;i++){
            leftMax[i]=Math.max(leftMax[i-1],height[i-1]);
        }
        for(int i=len-2;i>=0;i--){
            rightMax[i]=Math.max(rightMax[i+1],height[i+1]);
        }
        for(int i=0;i<len;i++){
            rightMax[i]=Math.min(rightMax[i],leftMax[i])-height[i];
            if(rightMax[i]>0) res+=rightMax[i];
        }
        return res;*/

        //此解法的时间复杂度是O(n),只需要遍历一遍。空间复杂度是O(1)
        //思路是采用l和r两个指针,维护装水两边的位置。
        //当l处高度低时,说明l右侧装的水肯定和l处一样高,此时逐步右移l,同是加上l处与右移后位置高度差(因为这里都能装水啊),直到再遇到同样         //高或者更高的位置。然后进行下一轮判断。
        //同样,当r处高度低时,说明r左侧的水肯定和r处一样高,此时逐步左移r,同是加上r处与左移后位置高度差,直到再遇到同样高或者更高的位置。
        //最后直到l和r相遇,结束。
        int res=0;
        int l=0;
        int len=height.length;
        int r=len-1;
        while(l<r){
            int temp=Math.min(height[l],height[r]);
            if(temp==height[l]){
                l++;
                while(l<r&&height[l]<=temp){
                    res+=temp-height[l];
                    l++;
                }

            }
            else{
                 r--;
                 while(l<r&&height[r]<=temp){
                    res+=temp-height[r];
                    r--;
                }

            }
        }
        return res;
    }
}
时间: 2024-10-13 01:31:38

[leedcode 42] Trapping Rain Water的相关文章

No.42 Trapping Rain Water

1 class Solution 2 { 3 public: 4 int trap(vector<int> &height) 5 {/* 6 雨水覆盖问题: 7 每一个bar能承受的水量为:它左侧所有中最高的和右侧所有中最高的中取最小值作为一个瓶颈[否则也留不住], 8 若该值大于当前bar的高度,其差值即为所求 9 累加所有bar能承受的水量即为所求 10 法三:[左右两个指针] 11 找到最高的位置,将数组分为两部分: 12 对左侧数据,因为右边已经有了最高值,所以,类似法一,找到

42. Trapping Rain Water(js)

42. 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. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1].

刷题42. Trapping Rain Water

一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的解法 这个题目是找"坑",然后计算里面可以存的"雨".总共提交了5次,前面几次都是边界错误. 代码如下: #include<iostream> #include<vector> using namespace std; class Solutio

[Leetcode][Python]42: Trapping Rain Water

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 42: Trapping Rain Waterhttps://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

11 Container With Most Water 42.Trapping Rain Water

11 和 42 本质上都是木桶原理: 11 如何才能存最多的水? 假设 a[left] < a[right] ,    total = a[left] *(right-left) ,  那么 right -1, right-2 位置 都比 total 小, 此时就没必要move right 了, 因为所有的right -x 都比 right 小. 此时只需要move left 指针, 继续找最大value. code 很简单: public int maxArea(int[] height) {

leetCode 42.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

42. 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 is represented

42. Trapping Rain Water *HARD*

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 a

【一天一道LeetCode】#42. Trapping Rain Water

一天一道LeetCode系列 (一)题目 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 m