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