728x90
LeetCode-42
Trapping Rain Water : Trapping rain water. find volume
note : Tip: swap
Answer
:::python
class Solution:
def trap(self, height: List[int]) -> int:
if not height:
return 0
volume = 0
left, right = 0, len(height) - 1
left_max, right_max = height[left], height[right]
while left < right:
left_max, right_max = max(height[left], left_max), max(height[right], right_max)
if left_max <= right_max:
volume += left_max - height[left]
left += 1
else:
volume += right_max - height[right]
right -= 1
return volume
Result : 126ms Memory: 16mb
๋ฐ์ํ
'๐ข One step' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetcode-561] Array Partition (0) | 2023.03.07 |
---|---|
[leetcode-15] Three Sum (0) | 2023.03.06 |
[leetcode-1] Two Sum (0) | 2023.03.04 |
[leetcode-5] Longest Palindrome Substring (0) | 2023.03.04 |
[leetcode-49] Group Anagrams (0) | 2023.03.02 |