A conveyor belt has packages that must be shipped from one port to another within days days.
The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). It is not allowed to load weight more than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.
Example 1:
Input: weights = [2,4,6,1,3,10], days = 4
Output: 10Explanation:
1st day: [2]
2nd day: [4,6]
3rd day: [1,3]
4th day: [10]
Example 2:
Input: weights = [1,2,3,4,5], days = 5
Output: 5Explanation:
1st day: [1]
2nd day: [2]
3rd day: [3]
4th day: [4]
5th day: [5]
Example 3:
Input: weights = [1,5,4,4,2,3], days = 3
Output: 8Explanation:
1st day = [1,5]
2nd day = [4,4]
3rd day = [2,3]
Constraints:
1 <= days, weights.length <= 50,0001 <= weights[i] <= 500Before attempting this problem, you should be comfortable with:
The minimum possible ship capacity must be at least as large as the heaviest package (otherwise that package could never be shipped). Starting from this minimum value, we can try each capacity one by one, simulating the shipping process to see if all packages can be delivered within the given number of days. The first capacity that works is our answer.
Instead of checking every capacity linearly, we can use binary search because the problem has a monotonic property: if a capacity works, any larger capacity will also work. The search space ranges from the maximum package weight (minimum valid capacity) to the sum of all weights (shipping everything in one day). For each mid-point capacity, we check if it allows shipping within the day limit and adjust our search accordingly.
left = max(weights), right = sum(weights).left <= right:mid = (left + right) / 2.mid by greedily filling each day's ship.r = mid - 1).l = mid + 1).class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
l, r = max(weights), sum(weights)
res = r
def canShip(cap):
ships, currCap = 1, cap
for w in weights:
if currCap - w < 0:
ships += 1
if ships > days:
return False
currCap = cap
currCap -= w
return True
while l <= r:
cap = (l + r) // 2
if canShip(cap):
res = min(res, cap)
r = cap - 1
else:
l = cap + 1
return resThe minimum capacity must be at least max(weights) (to ship the heaviest package), and the maximum is sum(weights) (ship everything in one day). Starting from 0 or 1 leads to invalid states where some packages cannot be shipped.
# Wrong: minimum capacity too low
l, r = 1, sum(weights) # Should be: l = max(weights)A common mistake is not counting the first day or incrementing the day counter at the wrong time. The ship starts with capacity available on day 1, and a new day begins when the current package cannot fit.
This is a "find minimum satisfying condition" problem. When the capacity works, you should search left (r = mid - 1) to find smaller valid capacities. Searching right instead returns a suboptimal answer.