Medium
Capacity to Ship Packages Within D Days
Medium
0 submissions
25 coins
+100 XP
Array
Binary Search
Problem Description
A conveyor belt has packages that must be shipped from one port to another within `days` days.
The `i`th 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`). We may not load more weight 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 = [1,2,3,4,5,6,7,8,9,10], days = 5
Output: 15
Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days.
Day 1: 1, 2, 3, 4, 5 (total: 15)
Day 2: 6, 7 (total: 13)
Day 3: 8 (total: 8)
Day 4: 9 (total: 9)
Day 5: 10 (total: 10)
```
**Example 2:**
```
Input: weights = [3,2,2,4,1,4], days = 3
Output: 6
Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days.
Day 1: 3, 2 (total: 5)
Day 2: 2, 4 (total: 6)
Day 3: 1, 4 (total: 5)
```
**Example 3:**
```
Input: weights = [1,2,3,1,1], days = 4
Output: 3
Explanation: A ship capacity of 3 is the minimum to ship in <= 4 days.
```
Constraints
- `1 <= days <= weights.length <= 500`
- `1 <= weights[i] <= 500`
Need help?
Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.
Video Call
Whiteboard
Live Coding
Screen Share