Skip to content

Commit 5af4185

Browse files
add 1011-capacity-to-ship-packages-within-d-days.py
1 parent 42bffaf commit 5af4185

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def shipWithinDays(self, weights: List[int], days: int) -> int:
3+
l, r = max(weights), sum(weights)
4+
min_cap = r
5+
6+
def canShip(cap):
7+
ships, curCap = 1, cap
8+
for w in weights:
9+
if curCap - w < 0:
10+
ships += 1
11+
curCap = cap
12+
curCap -= w
13+
return ships <= days
14+
15+
while l <= r:
16+
cap = (l + r) // 2
17+
if canShip(cap):
18+
min_cap = min(min_cap, cap)
19+
r = cap - 1
20+
else:
21+
l = cap + 1
22+
23+
return min_cap
24+
25+
26+

0 commit comments

Comments
 (0)