题解 | #接雨水问题#

接雨水问题

https://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * max water
     * @param arr int整型vector the array
     * @return long长整型
     */
    long long maxWater(vector<int>& arr) {
        // write code here
        int left=0,right=arr.size()-1,res=0;
        int lmax=0,rmax=0;
        while(left<right){
            lmax=max(lmax,arr[left]);
            rmax=max(rmax,arr[right]);
            if(lmax<rmax){
                res+=lmax-arr[left];
                left++;
            }
            else {
                res+=rmax-arr[right];
                right--;
            }
            
        }
        return res;
        
    }
};

全部评论

相关推荐

苗条的伊泽瑞尔最喜欢...:同28届被压力了,电科✌就不能去卷算法吗?把Java留给我们双非卷
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务