1. 程式人生 > >Leetcode trapping rain water 容器裝水問題

Leetcode trapping rain water 容器裝水問題

<p style="margin-top: 0px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: 'Microsoft YaHei', 'Helvetica Neue', SimSun; font-size: 14px; background-color: rgb(226, 226, 226);"></p>求陣列組成的凹槽能盛水的容積
主要有三種方法,之前嘗試兩種方法都會超時,最後一種方法思路:從兩端向中間靠攏,求以兩端為邊的容器能盛水的容積,然後所有值都剪去短邊的值,找到新的兩邊,不斷迭代下去直到中間。
設定左右兩個指標,找到第一個高度不為0的左右指標,找到其中的最小值,從左指標遍歷到有指標,若有比最小值小的,則容積加上最小值減去這個更小的值,且將元素值設定為0,否則將所有元素的值都剪去最小值,繼續外面的while迴圈,直到找到中間元素,這樣就減少了不必要的遍歷。
另外兩個方法分別是分別記錄兩個陣列,表示每個元素左邊最大值,和右邊高度最大值,然後利用左右最大值中的最小值減去元素本身值,結果若大於0,則加入容積,否則繼續遍歷,直到陣列長度遍歷完畢;另外方法就是從左到最高處,從右到最高處的遍歷方式,容積的加入和上面的類似,但是存在超時問題。
按照第一種方法,程式碼如下:
public class Solution {
    public int trap(int[] A) {
        if(A.length<3)
          return 0;
        int l = 0;
        int r = A.length-1;
        int water = 0;
        while(l<r) {
            while(l<r&&A[l]==0) l++;
            while(l<r&&A[r]==0) r--;
           
            int high = 0;
            int min = Math.min(A[l],A[r]);
            for(int i = l;i<=r;i++) {
                if(A[i]>=min){
                    A[i] -=min;
                }else{
                    high += min-A[i];
                    A[i] = 0;
                }
            }
            water += high;
        }//while
        return water;
    }
}


相關推薦

Leetcode trapping rain water 容器問題

<p style="margin-top: 0px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; border: 0px

LeetCode - Trapping Rain Water

size height ges tro turn idt ron href repr 題目描述: Given n non-negative integers representing an elevation map where the width of each bar

python leetcode Trapping Rain Water II

剛開始以為和Trapping Rain 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 elevation map, compute the volume of water it is able to tra

[LeetCode] 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. F

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 afte

[LeetCode][Java] Trapping Rain Water

這就是 enter rgb code sdn mil net line repr 題意: Given n non-negative integers representing an elevation map where the width of each bar

leetcode 42. Trapping Rain Water

pre lan pub after lin ret src esc problem link Given n non-negative integers representing an elevation map where the width of each bar is

LeetCode - 42、407 - Trapping Rain Water I - II

本文總結以下兩道題目: 42. Trapping Rain Water - 直方圖凹陷接雨水問題 (Hard) 407. Trapping Rain Water II - 上題二維變三維 (Hard) 42. Trapping Rain Water Gi

#Leetcode# 42. Trapping Rain Water

https://leetcode.com/problems/trapping-rain-water/   Given n non-negative integers representing an elevation map where the width of each b

python leetcode 42. Trapping Rain Water

首先最左邊和最右邊肯定無法蓄水 考慮能蓄水的情況:左右兩邊的高度都大於height[i] 所以得左右遍歷找到heigh[i]時的左右最大值 再遍歷height 當min(maxL[i],maxR[i])>height[i]即能蓄水 class Solution: def t

[leetcode]42. Trapping Rain Water

我這個腦子哦,暴力和動態規劃都有點思路,然後就是想不全。 想複雜了,一個一個高度減不就好了,我在那裡總面積減。 下面幾種方法都是一個一個豎的小面積算的。 Solution 1:暴力解法 時間複雜度o(n*n) 空間複雜度o(1) 從i往兩邊找最大 class Solution

LeetCode 題解:42. Trapping Rain Water

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

leetcode-11 容器最多

連結  測試用例: Input: [1,8,6,2,5,4,8,3,7] Output: 49 int maxArea(vector<int>& height) { int maxWater = 0; int i = 0, j = heigh

leetcode-42-Trapping Rain Water

The intuition behind it is use stack to do it. Instead of compute the area vertical, we compute the area horizontal. I.e. Base on that, we us

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

Leetcode 407. Trapping Rain Water II Leetcode 42. Trapping Rain Water

Problem: 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

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 af

LeetCode 接雨水 Trapping Rain Water 積水問題

中文題目使用兩根指標:給定 n 個非負整數表示每個寬度為1的柱子的高度圖,計算下雨之後能接多少水。給定 n 個非負整數表示每個寬度為1的柱子的高度圖,計算下雨之後能接多少水。例如,輸入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6。上面的高度圖由陣列 [0,1

19.2.4 [LeetCode 42] Trapping Rain Water

continue pin water 沒有 tor open bar solution array Given n non-negative integers representing an elevation map where the width of each bar