일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Generator
- Protocol
- 운영체제
- Python
- Class
- Python Code
- 30. Substring with Concatenation of All Words
- 파이썬
- kaggle
- Decorator
- 315. Count of Smaller Numbers After Self
- attribute
- 프로그래머스
- Python Implementation
- Regular Expression
- 109. Convert Sorted List to Binary Search Tree
- DWG
- 43. Multiply Strings
- shiba
- 715. Range Module
- Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- t1
- data science
- 시바견
- iterator
- concurrency
- 밴픽
- Substring with Concatenation of All Words
- LeetCode
Archives
- Today
- Total
Scribbling
LeetCode: 42. Trapping Rain Water 본문
The key observations to solve this problems:
- right >= left + 2, to contain water
- if we have a max height either at left or right position, then we can calculate water in the opposite position.
class Solution:
def trap(self, height: List[int]) -> int:
ret = 0
l, r = 0, len(height) - 1
leftmax, rightmax = height[l], height[r]
while r - l >= 2:
curmax = max(leftmax, rightmax)
if curmax == rightmax:
l += 1
if height[l] < leftmax:
ret += leftmax - height[l]
leftmax = max(leftmax, height[l])
else:
r -= 1
if height[r] < rightmax:
ret += rightmax - height[r]
rightmax = max(rightmax, height[r])
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 45. Jump Game II (0) | 2021.09.13 |
---|---|
LeetCode: 43. Multiply Strings (0) | 2021.09.13 |
34. Find First and Last Position of Element in Sorted Array (0) | 2021.09.11 |
33. Search in Rotated Sorted Array (0) | 2021.09.11 |
LeetCode: 31. Next Permutation (0) | 2021.09.09 |