nathanwailes

LeetCode 42 - Trapping Rain Water - NeetCode solution

Oct 10th, 2023
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. class Solution:
  2.     def trap(self, height: List[int]) -> int:
  3.        
  4.         if not height: return 0
  5.  
  6.         l, r = 0, len(height) - 1
  7.         leftMax, rightMax = height[l], height[r]
  8.         res = 0
  9.  
  10.         while l < r:
  11.             if leftMax < rightMax:
  12.                 l += 1
  13.                 leftMax = max(leftMax, height[l])
  14.                 res += leftMax - height[l]
  15.             else:
  16.                 r -= 1
  17.                 rightMax = max(rightMax, height[r])
  18.                 res += rightMax - height[r]
  19.        
  20.         return res
Advertisement
Add Comment
Please, Sign In to add comment