#42
Trapping Rain Water
hard· Two Pointersruns: 0Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 593
class Solution:
def trap(self, height: list[int]) -> int:
if not height:
return 0
left, right = 0, len(height) - 1
left_max, right_max = height[left], height[right]
water = 0
while left < right:
if left_max < right_max:
left += 1
left_max = max(left_max, height[left])
water += left_max - height[left]
else:
right -= 1
right_max = max(right_max, height[right])
water += right_max - height[right]
return water
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.