首页 > 其他 > 详细

【Leetcode刷题】接雨水

时间:2020-04-04 20:56:01      阅读:62      评论:0      收藏:0      [点我收藏+]

题目:https://leetcode-cn.com/problems/trapping-rain-water/

class Solution:
    def trap(self, height: List[int]) -> int:
        # 每一个位置能接的水 = min(左边最高,右边最高)- 当前位置高度
        N = len(height)
        if N == 0:
            return 0
        left_max = 0
        right_max = max(height)
        result = 0
        for i in range(N):
            h = height[i]
            if h == right_max:
                right_max = max(height[i+1:]) if i+1 < N else 0
            floor = min(left_max, right_max)
            if h < floor:
                result += floor - h
            if h > left_max:
                left_max = h
        return result

【Leetcode刷题】接雨水

原文:https://www.cnblogs.com/luozx207/p/12633721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!