일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 315. Count of Smaller Numbers After Self
- kaggle
- Python
- 컴퓨터의 구조
- 109. Convert Sorted List to Binary Search Tree
- DWG
- Generator
- 파이썬
- iterator
- t1
- 43. Multiply Strings
- Substring with Concatenation of All Words
- Class
- concurrency
- 30. Substring with Concatenation of All Words
- Decorator
- 프로그래머스
- Python Implementation
- Python Code
- 715. Range Module
- shiba
- Convert Sorted List to Binary Search Tree
- 밴픽
- data science
- Protocol
- Regular Expression
- 시바견
- LeetCode
- attribute
- 운영체제
Archives
- Today
- Total
Scribbling
LeetCode: 1011. Capacity To Ship Packages Within D Days 본문
Computer Science/Coding Test
LeetCode: 1011. Capacity To Ship Packages Within D Days
focalpoint 2021. 12. 6. 20:05척보면 binary search 문제이다.
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
max_capacity = 0
for w in weights:
max_capacity += w
ret = max_capacity
l, r = 0, max_capacity
while l <= r:
capacity = (l + r) // 2
if self.isEnoughCapacity(weights, capacity, days):
ret = min(ret, capacity)
r = capacity - 1
else:
l = capacity + 1
return ret
def isEnoughCapacity(self, weights, capacity, days):
if capacity < max(weights):
return False
num_days = 1
tempsum = 0
for w in weights:
tempsum += w
if tempsum > capacity:
num_days += 1
tempsum = w
return (num_days <= days)
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 212. Word Search II (0) | 2021.12.08 |
---|---|
LeetCode: 210. Course Schedule II (0) | 2021.12.07 |
LeetCode: 204. Count Primes (0) | 2021.12.05 |
LeetCode: 198. House Robber (0) | 2021.12.04 |
LeetCode: 179. Largest Number (0) | 2021.12.04 |