Skip to content

Commit f251ac9

Browse files
committed
1011
1 parent c8093d9 commit f251ac9

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
"""
3+
CREATED AT: 2023-03-06
4+
5+
URL: https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/
6+
7+
GITHUB: https://github.com/Jiezhi/myleetcode
8+
9+
FileName: 1011-CapacityToShipPackagesWithinDDays
10+
11+
Difficulty: Medium
12+
13+
Desc:
14+
15+
Tag:
16+
17+
See:
18+
19+
"""
20+
from tool import *
21+
22+
class Solution:
23+
def shipWithinDays(self, weights: List[int], days: int) -> int:
24+
def canship(cap: int) -> bool:
25+
took_day, took_cap = 1, 0
26+
for weight in weights:
27+
if took_cap + weight > cap:
28+
took_day += 1
29+
took_cap = weight
30+
if took_day > days:
31+
return False
32+
else:
33+
took_cap += weight
34+
35+
return took_day <= days
36+
37+
low, high = max(weights), sum(weights) + 1
38+
while low < high:
39+
mid = (high + low) // 2
40+
if canship(mid):
41+
high = mid
42+
else:
43+
low = mid + 1
44+
return low
45+
46+
47+
def test():
48+
assert Solution().shipWithinDays(weights = [1,2,3,4,5,6,7,8,9,10], days = 5) == 15
49+
50+
51+
if __name__ == '__main__':
52+
test()
53+

0 commit comments

Comments
 (0)