| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- data science
- 715. Range Module
- iterator
- 30. Substring with Concatenation of All Words
- LeetCode
- Generator
- 밴픽
- Convert Sorted List to Binary Search Tree
- kaggle
- DWG
- Protocol
- attribute
- 시바견
- 315. Count of Smaller Numbers After Self
- Decorator
- 컴퓨터의 구조
- t1
- Class
- Python Code
- Python
- 파이썬
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- 프로그래머스
- 운영체제
- 43. Multiply Strings
- concurrency
- Substring with Concatenation of All Words
- shiba
- Regular Expression
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 |