美文网首页
2020-03-14 Day21 Leetcode: 42. T

2020-03-14 Day21 Leetcode: 42. T

作者: YueTan | 来源:发表于2020-03-14 08:33 被阅读0次
class Solution:
    def trap(self, height: List[int]) -> int:
        if len(height)<3:
            return 0
        
        left_max=[0]
        right_max=[0]
        
        max_value=height[0]
        for i in range(1,len(height)):
            if height[i]>max_value:
                max_value=height[i]
            left_max.append(max_value)
            
        max_value=height[-1]
        for i in range(len(height)-1,-1,-1):
            if height[i]>max_value:
                max_value=height[i]
            right_max.insert(0,max_value)
        
        water_nums=[]
        for i in range(len(height)):
            water_per=max(min(left_max[i],right_max[i])-height[i],0)
            water_nums.append(water_per)
        
        return sum(water_nums)
            
        
            
        
        

相关文章

网友评论

      本文标题:2020-03-14 Day21 Leetcode: 42. T

      本文链接:https://www.haomeiwen.com/subject/aqtmshtx.html