Time: 42 ms (98.41%), Space: 18.5 MB (34.38%) - LeetHub

This commit is contained in:
Deven
2024-10-20 14:02:39 -04:00
parent e439861a12
commit 9380fbffb7
@@ -0,0 +1,18 @@
class Solution:
def trap(self, height: List[int]) -> int:
left = [0] * len(height)
right = [0] * len(height)
waterGathered = 0
for i in range(len(height)):
if i > 0:
left[i] = max(left[i-1], height[i-1])
backI = len(height) - i - 1
if backI < len(height) - 1:
right[backI] = max(right[backI + 1], height[backI + 1])
for i in range(len(height)):
waterGathered += max(0, min(left[i], right[i]) - height[i])
return waterGathered