题解 | #接雨水问题#

接雨水问题

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

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# max water
# @param arr int整型一维数组 the array
# @return long长整型
#
class Solution:
    def maxWater(self , arr: List[int]) -> int:
        # write code here
        left = right = preMax = sufMax = 0
        right = len(arr) - 1
        ans = 0

        while left < right:
            preMax = max(preMax, arr[left])
            sufMax = max(sufMax, arr[right])
            if preMax <= sufMax:
                ans += preMax - arr[left]
                left += 1
            else:
                ans += sufMax - arr[right]
                right -= 1
        return ans

算法刷题记录 文章被收录于专栏

刷题,记录牛客的101

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 20:15
还能挽救吗?找同学帮忙看了一下&nbsp;字节怎么能如此对我
牛客26396789...:你这是严重红线,被发现你自己永远进不去,你那个同学直接走人,你还敢宣扬
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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