码迷,mamicode.com
首页 > 其他好文 > 详细

【Leetcode刷题】接雨水

时间:2020-04-04 21:11:27      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:min   return   刷题   etc   app   ret   lis   problem   turn   

题目: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刷题】接雨水

标签:min   return   刷题   etc   app   ret   lis   problem   turn   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!