An easy understanding Python solution beat 99.86%


  • -1
    H

    '''
    def trap(self, height):
    if len(height) < 2: return 0
    l = 0
    r = len(height) - 1
    if height[l] < height[r]:
    mid, d = 1, 1
    else:
    mid, d = r - 1, - 1
    water = 0
    while l < mid < r:
    if d == 1:
    if height[mid] < height[l]:
    water += height[l] - height[mid]
    mid += 1
    elif height[mid] <= height[r]:
    l = mid
    mid += 1
    else:
    d = -1
    l = mid
    mid = r - 1
    if d == -1:
    if height[mid] < height[r]:
    water += height[r] - height[mid]
    mid -= 1
    elif height[mid] <= height[l]:
    r = mid
    mid -= 1
    else:
    d = 1
    r = mid
    mid = l + 1
    return water
    '''


Log in to reply
 

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