lc42.java 2.0 KB
Newer Older
L
liu13 已提交
1
package code;
L
liu13 已提交
2 3 4

import java.util.Stack;

L
liu13 已提交
5 6 7 8 9
/*
 * 42. Trapping Rain Water
 * 题意:能盛多少水
 * 难度:Hard
 * 分类:Array, Two Pointers, Stack
L
liu13 已提交
10
 * 思路:三种方法:1.DP先求出来每个位置的maxleft,maxright,再遍历一遍;2.两个指针,类似lc11题的思路;3.用栈数据结构;
L
liu13 已提交
11
 * Tips:lc11, lc42, lc84
L
liu13 已提交
12 13 14 15 16
 */
public class lc42 {
    public static void main(String[] args) {
        int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
        System.out.println(trap(height));
L
liu13 已提交
17
        System.out.println(trap2(height));
L
liu13 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    }
    public static int trap(int[] height) {
        if(height.length<3)
            return 0;
        int left = 0;
        int right = height.length-1;
        int res =0;

        while(left<right){
            if(height[left]<height[right]){
                int edge_l = height[left];
                left++;
                while(height[left]<edge_l && left<right){
                    if(edge_l-height[left]>0)
                        res += edge_l-height[left];
                    left++;
                }
            }else if(height[left]>=height[right] && left<right){
                int edge_r = height[right];
                right--;
                while(height[right]<edge_r){
                    if(edge_r-height[right]>0)
                        res += edge_r-height[right];
                    right--;
                }
            }
        }
        return res;
    }
L
liu13 已提交
47 48 49 50 51 52 53 54

    public static int trap2(int[] A) {
        //栈方法
        if (A==null) return 0;
        Stack<Integer> s = new Stack<Integer>();
        int i = 0, maxWater = 0, maxBotWater = 0;
        while (i < A.length){
            if (s.isEmpty() || A[i]<=A[s.peek()]){
L
liu13 已提交
55
                s.push(i++);    //递减栈
L
liu13 已提交
56 57 58 59 60 61 62 63 64 65
            }
            else {
                int bot = s.pop();
                maxBotWater = s.isEmpty()? // empty means no il
                        0:(Math.min(A[s.peek()],A[i])-A[bot])*(i-s.peek()-1);
                maxWater += maxBotWater;
            }
        }
        return maxWater;
    }
L
liu13 已提交
66
}