题解 | #接雨水问题#

接雨水问题

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

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * max water
     * @param arr int整型一维数组 the array
     * @return long长整型
     */
    public long maxWater (int[] arr) {
        // write code here
        long res = 0;
        if(arr.length < 3){
            return res;
        }

        // 指针定义
        int left = 0;
        int right = arr.length - 1;
        // 中间区域的边界高度
        int maxL = 0;
        int maxR = 0;
        while(left < right){
            // 每次维护最新的最大边界
            maxL = Math.max(arr[left],maxL);
            maxR = Math.max(arr[right],maxR);

            if(maxL < maxR){
                res += maxL - arr[left++];
            }else{
                res += maxR - arr[right--];
            }
        }
        return res;
    }
}

全部评论

相关推荐

写不来代码的小黑:这么小的城市能有做it的公司也不容易
点赞 评论 收藏
分享
DKS233:项目写太简单了,你用什么技术实现了什么功能,优化了多少,分了哪些模块,解决了哪些难点,最好分模块写,你写的太模糊了。精通还是少用吧,你确定问你底层你扛的住吗,最好用熟悉。具备良好**意识,这种空话不要写,技能层面,要写就写实在的,比如“熟悉常用数据结构,如,堆,栈,链表,哈希表,平衡树”这种
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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