题解 | #接雨水问题#

接雨水问题

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

lmax是arr[0...lo]中最高柱子的高度,rmax是arr[hi...]中最高柱子的高度

#
# max water
# @param arr int整型一维数组 the array
# @return long长整型
#
class Solution:
    def maxWater(self , arr ):
        # write code here
        n = len(arr)        
        lo, hi = 0, n-1
        lmax, rmax = arr[0], arr[n-1]
        res = 0
        while lo <= hi:
            lmax = max(lmax, arr[lo])
            rmax = max(rmax, arr[hi])

            if lmax > rmax:
                res += max(0, (rmax-arr[hi]))
                hi -= 1
            else:
                res += max(0, (lmax-arr[lo]))
                lo += 1
        return res
全部评论

相关推荐

09-21 21:14
门头沟学院
否极泰来来来来:和他说:这里不好骂你,我们加个微信聊
点赞 评论 收藏
分享
迷茫的大四🐶:我不许你接受,我不许你启动咏鹅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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