1. 程式人生 > >Leetcode 42 Trapping Rain 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 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 rain water (blue section) are being trapped. Thanks Marcos

 for contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

這個題的意思是計算可以盛水的多少,可以使用雙指標,單次迴圈法,壓棧方法。

1)

class Solution {
    public int trap(int[] height) {
        if(height == null && height.length == 0){
            return 0;
        }
        int size = 0;
        int max = 0;
        int[] column = new int[height.length];
        
        for(int i = 0 ; i < height.length; ++i){
            column[i] = max;
            max = Math.max(max,height[i]);
        }
        max = 0;
        for(int i = height.length - 1 ; i >= 0; --i){
            column[i] = Math.min(max,column[i]);
            max = Math.max(max,height[i]);
            size += column[i] > height[i] ? Math.abs(column[i] - height[i]) : 0;
        }
        return size;
    }
}

2)雙指標

class Solution {
    public int trap(int[] A) {
        int n = A.length;
        int left = 0, right = n - 1;
        int maxLeft = 0, maxRight = 0;
        int ans = 0;
        while (left < right) {
            if (A[left] < A[right]) {
                if (A[left] >= maxLeft) {
                    maxLeft = A[left];
                } else {
                    ans += maxLeft - A[left];
                }
                left ++;
            } else {
                if (A[right] >= maxRight) {
                    maxRight = A[right];
                } else {
                    ans += maxRight - A[right];
                }
                right --;
            }
        }
        return ans;
    }
}

3)

class Solution {
    //11.20
    public int trap(int[] height) {
        Stack<Integer> st = new Stack();
        int lastCounted = -1, lastCountedHeight, w,h;
        int area = 0;
        for (int i = 0; i< height.length; i++) {
            int curHeight = height[i];
            if (curHeight == 0)
                continue;
            
            while (!st.isEmpty()) {
                int lastBlock = st.peek();
              //  System.out.format("===%d %d %d %d %d %d\n", i, area, lastBlock,  height[lastBlock],curHeight,st.size());
                if (height[lastBlock] > curHeight) {
                    w = i - lastBlock - 1;
                    h = curHeight;//as curHeihgt is smaller
                    lastCountedHeight = 0;
                    if (lastCounted > lastBlock)
                        lastCountedHeight = height[lastCounted];
                    lastCounted = i;
                    area = area+ (w * h) - (w * lastCountedHeight);
                   // System.out.format("%d %d %d %d %d %d\n", i, area, w, h, lastCountedHeight,st.size());
                    st.push(i);
                    break;
                }
                st.pop();
                w = i - lastBlock - 1;
                h = height[lastBlock];
                lastCountedHeight = 0;
                if (lastCounted > lastBlock)
                    lastCountedHeight = height[lastCounted];
                lastCounted = lastBlock;
                area = area + (w * h) - (w * lastCountedHeight);
                
                //System.out.format("--%d %d %d %d %d  %d\n", i, area, w, h, lastCountedHeight, st.size());
            }
            //System.out.println(i);
            if (st.isEmpty()) {
                st.push(i);
            }
            
            
        }
        return area;
    }
}

注意細節,兩端的節點。

相關推薦

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

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

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

LeetCode 題解:42. Trapping Rain Water

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

LeetCode42. 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

42. Trapping Rain Water

turn blog length spa trapping cnblogs height water pub class Solution { public int trap(int[] height) { int sum=0;

42.trapping-rain-water

      以前在聽演算法課的時候,有一個人說過這樣一句話,凡是那道題沒有思路的9成要用動態規劃,當然可能說的有點絕對,但是不無道理,就是說,一般的題目我們遇到後能立即想到思路,哪怕是那種很麻煩的,但是動態規劃往往是麻煩的也不一定能想出來。而動態規劃的難度也確實比較大,

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

*42. Trapping Rain Water 接雨水

rain 情況下 temp n-1 寬度 最小 mar 當前位置 sel 1. 原始題目 給定 n 個非負整數表示每個寬度為 1 的柱子的高度圖,計算按此排列的柱子,下雨之後能接多少雨水。 上面是由數組 [0,1,0,2,1,0,1,3,2,1,2,1] 表示

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

42. 接雨水(Trapping rain water

題目描述 LeetCode.cn地址:https://leetcode-cn.com/problems/trapping-rain-water/ LeetCode地址:https://leetcode.com/problems/trapping-rain-water/ 思路

python leetcode Trapping Rain Water II

剛開始以為和Trapping Rain Water做法一樣 就是設定四個方向的最大值 但出錯了 錯誤原因:一維的話蓄水只能往左右擴充套件,但二維可以往四周擴充套件(可能是第一次是往右擴充套件第二次往下了第三次又往右了 即無法保持在同一緯度上擴充套件)所以無法簡單地求出當前的最大值 見程式碼二

【python/Hard/42Trapping Rain Water

題目 實現思路 模擬法。 開闢一個數組leftMaxArr,leftMaxArr[i]為height[i]之前的最高的bar值,然後從後面開始遍歷,用rightMax來記錄從後向前遍歷遇到