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) {

        int start = 0;
        int end = height.length-1;
        int max = 0;

        while(start< end) {
          if(height[start] < height[end]) {
              max = Math.max(max,(height[start]*(end-start)));
              start ++;
          }  

          else  {
              max = Math.max(max,height[end]*(end-start));
              end --;
          }
        }
        return max;
    }

42 题

随便一个 ai ,如果存在某个left 点   ai-left > ai  并且存在 right 点  ai-right >ai , 则 ai 的水可以存的住, 无论 left  是否是 i 的邻居, 隔着很远也能决定ai 能被存下来。   那能存多少呢? 存多少 取决于 ai-left or ai-right 的最小值, 这就是木桶原理。

那么从left 到right 扫描一遍, 找到每个点left 的max value ,存数组 leftMax[]    并且  从right  ->left 找每个点往right 看的max value.  存到一个数组里 rightMax[]

然后  能存住的水 就是 min(leftMax[i], rightMax[i]) - a[i]

code 也很简单:

    public int trap(int[] height) {

        if(height == null || height.length<3) return 0;

        int n = height.length;
        int[] leftMax = new int[n];
        int[] rightMax = new int[n];

        leftMax[0] = height[0];
        for(int i=1; i<n ; i++){
            leftMax[i] = Math.max(leftMax[i-1],height[i]);
        }

        rightMax[n-1] = height[n-1];
        for(int i = n-2; i>=0; i--){
            rightMax[i] = Math.max(rightMax[i+1],height[i]);
        }

        int ans = 0;

        for(int i=0; i<n; i++){
            ans += Math.min(leftMax[i],rightMax[i]) - height[i];
        }

        return ans;
    

时间 复杂度为 O(N) 空间复杂度也是 O(n) ,但实际并不用额外的空间。 用two point 方法,木桶原理, 维护一个 leftMax 以及 rightMax ,如果leftMax < rightMax , 只用move leftMax 就行了,然后 ans += Math.min(leftMax,rightMax) - height[i].

    public int trap(int[] height) {
       if(height == null || height.length <3) return 0; 

       int n = height.length;
       int leftBound = 0;
       int rightBound = 0;

       int left = 0;
       int right = n-1;
       int ans = 0 ;

       while(left<right){
           leftBound = Math.max(height[left],leftBound);
           rightBound = Math.max(height[right],rightBound);

           if(leftBound < rightBound){
              ans += leftBound - height[left];
              left++;
           }

           else {
               ans += rightBound - height[right];
               right--;
           }
       }

       return ans;
    }

407. Trapping Rain Water II

把42题的平面变成立体,方法完全变了。得用BFS来做, 可参考 https://www.cnblogs.com/grandyang/p/5928987.html

原文地址:https://www.cnblogs.com/keepAC/p/11605697.html

时间: 2024-12-11 15:06:24

11 Container With Most Water 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

leetcode之Container With Most Water 和Trapping Rain Water

Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together wi

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

leetcode problem 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 a

19.2.4 [LeetCode 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]. In this case, 6 units of

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