2020-03-14 Day21 Leetcode: 42. T

2020-03-14  本文已影响0人  YueTan
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)
            
        
            
        
        
上一篇 下一篇

猜你喜欢

热点阅读