Skip to content

Commit ccc5e59

Browse files
committed
Add 1011
1 parent 5263bfb commit ccc5e59

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
| 1004 | Max Consecutive Ones III | Medium | O(N) | O(1) | Sliding Window, Two Pointers | | |
247247
| 1007 | Minimum Domino Rotations For Equal Row | Medium | O(N) | O(1) | Array, Greedy | | |
248248
| 1010 | Pairs of Songs With Total Durations Divisible by 60 | Medium | O(N) | O(1) | Array, Math | | |
249+
| 1011 | Capacity To Ship Packages Within D Days | Medium | O(NlogN) | O(1) | Array, Binary Search | | |
249250
| 1014 | Best Sightseeing Pair | Medium | O(N) | O(1) | Array | | |
250251
| 1015 | Smallest Integer Divisible by K | Medium | O(K) | O(K) | Math | | |
251252
| 1022 | Sum of Root To Leaf Binary Numbers | Easy | O(N) | O(H) | Tree | | |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# @lc app=leetcode id=1011 lang=python3
3+
#
4+
# [1011] Capacity To Ship Packages Within D Days
5+
#
6+
7+
# @lc code=start
8+
# TAGS: Array, Binary Search
9+
class Solution:
10+
def shipWithinDays(self, weights: List[int], D: int) -> int:
11+
def within_D_days(cap, D):
12+
days = 1
13+
cur = 0
14+
for w in weights:
15+
if cur + w > cap:
16+
days += 1
17+
cur = 0
18+
cur += w
19+
return days <= D
20+
21+
lo = max(weights)
22+
hi = sum(weights)
23+
while lo < hi:
24+
mid = (lo + hi) // 2
25+
if within_D_days(mid, D):
26+
hi = mid
27+
else:
28+
lo = mid + 1
29+
return lo
30+
# @lc code=end
31+

0 commit comments

Comments
 (0)