trapping-rain-water py3

This commit is contained in:
2022-04-23 21:28:55 +05:30
parent a0b3f7f134
commit a115f39b17
4 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
# Time: O(N · H) where H is the maximum height
# Space: O(1)
class Solution:
def trap(self, height: List[int]) -> int:
maxh = 0
for h in height:
maxh = max(h, maxh)
land_width = len(height)
output = 0
for i in range(maxh):
row_output = 0
trough_output = 0
trough_start = -1
for j in range(land_width):
# A block is present
if height[j] >= i + 1:
row_output += trough_output
trough_start = j
trough_output = 0
elif trough_start >= 0:
trough_output += 1
output += row_output
return output