码迷,mamicode.com
首页 > 移动开发 > 详细

Trapping Rain Water

时间:2015-04-10 13:00:09      阅读:153      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!