trapping_rain_water
1# @leet start 2class Solution: 3 def trap(self, height: list[int]) -> int: 4 """ 5 This question asks us to see how much water can be trapped in between 6 bars, given their heights. 7 We can solve this using two pointers: 8 We close the window, and calculate the trapped water based on the 9 difference between the maximum height and the current height. 10 We update the maximum height each iteration. 11 If the right side is greater than the left side, we only look at the 12 left side, or vice versa. 13 """ 14 left, right = 0, len(height) - 1 15 ans = 0 16 left_max, right_max = 0, 0 17 18 while left < right: 19 left_height, right_height = height[left], height[right] 20 if left_height < right_height: 21 left_max = max(left_max, left_height) 22 ans += left_max - left_height 23 left += 1 24 else: 25 right_max = max(right_max, right_height) 26 ans += right_max - right_height 27 right -= 1 28 return ans 29 30 31# @leet end 32 33 34def test(): 35 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def trap(self, height: list[int]) -> int: 5 """ 6 This question asks us to see how much water can be trapped in between 7 bars, given their heights. 8 We can solve this using two pointers: 9 We close the window, and calculate the trapped water based on the 10 difference between the maximum height and the current height. 11 We update the maximum height each iteration. 12 If the right side is greater than the left side, we only look at the 13 left side, or vice versa. 14 """ 15 left, right = 0, len(height) - 1 16 ans = 0 17 left_max, right_max = 0, 0 18 19 while left < right: 20 left_height, right_height = height[left], height[right] 21 if left_height < right_height: 22 left_max = max(left_max, left_height) 23 ans += left_max - left_height 24 left += 1 25 else: 26 right_max = max(right_max, right_height) 27 ans += right_max - right_height 28 right -= 1 29 return ans
def
trap(self, height: list[int]) -> int:
4 def trap(self, height: list[int]) -> int: 5 """ 6 This question asks us to see how much water can be trapped in between 7 bars, given their heights. 8 We can solve this using two pointers: 9 We close the window, and calculate the trapped water based on the 10 difference between the maximum height and the current height. 11 We update the maximum height each iteration. 12 If the right side is greater than the left side, we only look at the 13 left side, or vice versa. 14 """ 15 left, right = 0, len(height) - 1 16 ans = 0 17 left_max, right_max = 0, 0 18 19 while left < right: 20 left_height, right_height = height[left], height[right] 21 if left_height < right_height: 22 left_max = max(left_max, left_height) 23 ans += left_max - left_height 24 left += 1 25 else: 26 right_max = max(right_max, right_height) 27 ans += right_max - right_height 28 right -= 1 29 return ans
This question asks us to see how much water can be trapped in between bars, given their heights. We can solve this using two pointers: We close the window, and calculate the trapped water based on the difference between the maximum height and the current height. We update the maximum height each iteration. If the right side is greater than the left side, we only look at the left side, or vice versa.
def
test():