Trapping Rain Water

感觉和

Container With Most Water

 很像,这里有个问题,如果让球最大的坑,怎么做呢?

还有candy那道题,好像都是这种双扫系列的

ref http://fisherlei.blogspot.com/2013/01/leetcode-trapping-rain-water.html 双扫

public class Solution {
    public int trap(int[] A) {
        if(A==null || A.length<3) return 0;
        int res=0, curV = 0, max=A[0];
        int[] mL = new int[A.length];
        int[] mR = new int[A.length];
        
        for(int i=1; i<A.length-1; i++){
            mL[i] = max;
            max = Math.max(A[i],max);
        }
        max = A[A.length-1];
        for(int j=A.length-2; j>=0; j--){
            mR[j] =max;
            max = Math.max(A[j], max);
            curV = Math.min(mL[j], mR[j])-A[j];
            if(curV>0) 
                res += curV;
        }
        
        return res;
    }
}

 

posted @ 2015-04-10 11:32  世界到处都是小星星  阅读(137)  评论(0编辑  收藏  举报