My easy understood one-pass 13 lines Python solution


  • 1
    W
    class Solution(object):
        def trap(self, height):
            """
            :type height: List[int]
            :rtype: int
            """
            if not height :
                return 0
            count = 0 
            
            ind = height.index(max(height))
            
            for i in range(ind):
                if height[i] > height[i+1]:
                    count += height[i] - height[i+1]
                    height[i+1] = height[i]
            
            for i in range(len(height) -1 , ind, -1):
                if height[i] > height[i-1]:
                    count += height[i] - height[i-1]
                    height[i-1] = height[i] 
            
            return count

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.