Skip to content

Commit 56e1843

Browse files
authored
Create capacity-to-ship-packages-within-d-days.py
1 parent b84dfe4 commit 56e1843

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(nlogr)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def shipWithinDays(self, weights, D):
6+
"""
7+
:type weights: List[int]
8+
:type D: int
9+
:rtype: int
10+
"""
11+
def possible(weights, D, mid):
12+
result, curr = 1, 0
13+
for w in weights:
14+
if curr+w > mid:
15+
result += 1
16+
curr = 0
17+
curr += w
18+
return result <= D
19+
20+
left, right = max(weights), sum(weights)
21+
while left <= right:
22+
mid = left + (right-left)//2
23+
if possible(weights, D, mid):
24+
right = mid-1
25+
else:
26+
left = mid+1
27+
return left

0 commit comments

Comments
 (0)