Skip to content

Commit

Permalink
Solution for problem 42 with Python
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-per-ardua committed Oct 30, 2023
1 parent 3618020 commit da0de20
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Hard/42. Trapping Rain Water/Approach 1/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def trap(self, height: List[int]) -> int:
if not height:
return 0
n = len(height)
lm = [0] * n
rm = [0] * n

lm[0] = height[0]
for i in range(1, n):
lm[i] = max(lm[i - 1], height[i])

rm[n - 1] = height[n - 1]
for i in range(n - 2, -1, -1):
rm[i] = max(rm[i + 1], height[i])

water = 0
for i in range(n):
water += min(lm[i], rm[i]) - height[i]

return water

0 comments on commit da0de20

Please sign in to comment.