-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1011.py
28 lines (26 loc) · 887 Bytes
/
1011.py
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
# https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
from typing import List
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
maxWeight = max(weights)
l, r = maxWeight, -(len(weights) // -days)* maxWeight
minCap = float('inf')
while l <= r:
cap = (l + r) // 2
cycles = 0
currCap = 0
for w in weights:
currCap += w
if currCap > cap:
cycles += 1
currCap = w
elif currCap == cap:
cycles += 1
currCap = 0
cycles += 1 if currCap else 0
if cycles <= days:
minCap = cap
r = cap - 1
else:
l = cap + 1
return minCap