LC042 - Trapping Rain Water
Problem
Example
Solution
def trap(self, height: List[int]) -> int:
# case empty
if not height:
return 0
res = 0
left = 0
right = len(height) - 1
maxL, maxR = height[left], height[right]
while left < right:
if maxL < maxR:
res += maxL - height[left]
left += 1
maxL = max(maxL, height[left])
else:
res += maxR - height[right]
right -= 1
maxR = max(maxR, height[right])
return resLast updated