[LintCode/LeetCode] Trapping Rain Water [棧和雙指針]

Problem

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

Example

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

Note

有兩種方法。一種是利用Stack去找同一層的兩個邊,不斷累加寄存。如[2, 1, 0, 1, 2],2入棧,1入棧,0入棧,下一個1大於棧頂元素0,則計算此處的雨水量加入res,此過程當中0從棧中彈出,1入棧,到下一個2,先彈出1,因爲以前還有一個1在棧中,因此計算時高度的因數相減爲0,雨水量爲0,res無變化,繼續pop出棧中的元素,也就是以前的1,此時stack中仍有元素2,說明左邊還有邊,繼續計算底層高度爲1,兩個值爲2的邊之間的水量,加入res。
雙指針法的思想:先找到左右兩邊的第一個峯值做爲參照位,而後分別向後(向前)每一步增長該位與參照位在這一位的差值,加入sum,直到下一個峯值,再更新爲新的參照位。這裏有一個須要注意的地方,爲何要先計算左右兩個峯值中較小的那個呢?由於在兩個指針逼近中間組成最後一個積水區間時,要用較短邊計算。指針

Solution

1. Stackcode

public class Solution {
    public int trapRainWater(int[] A) {
        Stack<Integer> stack = new Stack<Integer>();
        int res = 0;
        for (int i = 0; i < A.length; i++) {
            if (stack.isEmpty() || A[i] < A[stack.peek()]) stack.push(i);
            else {
                while (!stack.isEmpty() && A[i] > A[stack.peek()]) {
                    int pre = stack.pop();
                    if (!stack.isEmpty()) {
                        res += (Math.min(A[i], A[stack.peek()]) - A[pre]) * (i - stack.peek() - 1);
                    }
                }
                stack.push(i);
            }
        }
        return res;
    }
}

2. Two-Pointerhtm

public class Solution {
    public int trap(int[] A) {
        int left = 0, right = A.length-1, res = 0;
        while (left < right && A[left] < A[left+1]) left++;
        while (left < right && A[right] < A[right-1]) right--;
        while (left < right) {
            int leftmost = A[left], rightmost = A[right];
            if (leftmost < rightmost) {
                while (left < right && A[++left] < leftmost) res += leftmost - A[left];
            }
            else {
                while (left < right && A[--right] < rightmost) res += rightmost - A[right];
            }
        }
        return res;
    }
}

雙指針法update: 2018-3

public class Solution {
    public int trap(int[] A) {
        if (A == null || A.length < 3) return 0;
        
        //set left/right pointers
        int l = 0, r = A.length-1;
        
        //find the first left/right peaks as left/right bounds
        while (l < r && A[l] <= A[l+1]) l++;
        while (l < r && A[r] <= A[r-1]) r--;
        
        //output
        int trappedWater = 0;
        
        //implementation
        while (l < r) {
            
            int left = A[l];
            int right = A[r];
             
            //when you have a higher RIGHT bound...
            if (left <= right) {

             //when 'l' is highest left bound
             //it's safe to add trapped water RIGHTward
             while (l < r && left >= A[l+1]) {
                 l++;
                 trappedWater += left - A[l];
             }

             //when left pointer 'l' found 'l+1' a higher left bound
             //reset the left bound
             l++;
            } 

            
            //when you have a higher LEFT bound...
            else {

             //when 'r' is highest right bound
             //it's safe to add trapped water LEFTward
             while (l < r && right >= A[r-1]) {
                 r--;
                 trappedWater += right - A[r];
             }

             //when right pointer 'r' found 'r-1' a higher right bound
             //reset the right bound
             r--;
            }
        }
        
        return trappedWater;
    }
}
相關文章
相關標籤/搜索