Trapping rain water
Given 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.
#dynamic programming method
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
if height == []:
return 0
n = len(height)
max_left = [0]* n
max_right = [0]* n
##print(max_left, height)
max_left[0] = 0
max_right[n-1] = 0
for i in range (1, n):
max_left[i] = max(max_left[i - 1], height[i-1])
for i in range(n-2, -1, -1):
max_right[i] = max(max_right[i + 1], height[i + 1])
output = 0
print(max_left, max_right)
for i in range(n):
lower_boundary = min(max_left[i], max_right[i])
max_trap_at_i = lower_boundary - height[i]
if max_trap_at_i > 0:
output += max_trap_at_i
return output
Two Pointer method:
class Solution:
def trap(self, height: List[int]) -> int:
ans = 0
l,r = 0 , len(height) -1
l_max, r_max = 0,0
while l < r:
# case 1: lower_bound is from left.
if height[l] < height[r]:
if height[l] >= l_max:
l_max = height[l]
else:
ans += l_max - height[l]
l += 1
# case 2: the lower_bound is from right
else:
if height[r] >= r_max:
r_max = height[r]
else:
ans += r_max - height[r]
r -= 1
return ans
Posted by Jamie Meyer 18 days ago