1011. Capacity To Ship Packages Within D Days
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 72% Topics: Array, Binary Search
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogr)
# Space: O(1)
class Solution(object):
def shipWithinDays(self, weights, D):
"""
:type weights: List[int]
:type D: int
:rtype: int
"""
def possible(weights, D, mid):
result, curr = 1, 0
for w in weights:
if curr+w > mid:
result += 1
curr = 0
curr += w
return result <= D
left, right = max(weights), sum(weights)
while left <= right:
mid = left + (right-left)//2
if possible(weights, D, mid):
right = mid-1
else:
left = mid+1
return left
Solution from kamyu104/LeetCode-Solutions · MIT