首页 > 移动平台 > 详细

Trapping Rain Water

时间:2015-04-10 12:58:19      阅读:208      评论:0      收藏:0      [点我收藏+]

感觉和

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

 

Trapping Rain Water

原文:http://www.cnblogs.com/jiajiaxingxing/p/4414088.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!