class Solution:
def trap(self, height: List[int]) -> int:
if len(height) < 2:
return 0
total_max_height = max(height)
total_water = 0
left_index = 0
right_index = len(height)-1
left_max_height = height[left_index]
right_max_height = height[right_index]
while left_max_height < total_max_height:
left_max_height = max(height[left_index], left_max_height)
if left_max_height > height[left_index]:
total_water += left_max_height - height[left_index]
left_index += 1
while right_max_height < total_max_height:
right_max_height = max(height[right_index], right_max_height)
if right_max_height > height[right_index]:
total_water += right_max_height - height[right_index]
right_index -= 1
return total_water
class Solution:
def trap(self, height: List[int]) -> int:
if len(height) < 2:
return 0
total_max_height = max(height)
total_water = 0
left_index = 0
right_index = len(height)-1
left_max_height = height[left_index]
right_max_height = height[right_index]
while right_index > left_index:
right_max_height = max(height[right_index], right_max_height)
left_max_height = max(height[left_index], left_max_height)
if(right_max_height <= left_max_height):
if(right_max_height > height[right_index]):
total_water += right_max_height - height[right_index]
right_index -= 1
else:
if(left_max_height > height[left_index]):
total_water += left_max_height - height[left_index]
left_index += 1
return total_water