> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Capacity To Ship Packages Within D Days

> Tested Python solution for LeetCode 1011 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1011, Medium. Topics: Array, Binary Search. [View on LeetCode](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1011   # by problem number
lcpy gen -s capacity_to_ship_packages_within_d_days   # by problem name
```

## Problem

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.

### Examples

```
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 like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10
```

```
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 like this:
1st day: 3, 2
2nd day: 2, 4
3rd day: 1, 4
```

```
Input: weights = [1,2,3,1,1], days = 4
Output: 3
```

### Constraints

* 1 \<= days \<= weights.length \<= 5 \* 10^4
* 1 \<= weights\[i] \<= 500

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/capacity_to_ship_packages_within_d_days/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/capacity_to_ship_packages_within_d_days/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log(sum(weights)))
    # Space: O(1)
    def ship_within_days(self, weights: list[int], days: int) -> int:
        def can_ship(capacity: int) -> bool:
            needed_days = 1
            current_load = 0
            for weight in weights:
                if current_load + weight > capacity:
                    needed_days += 1
                    current_load = 0
                current_load += weight
            return needed_days <= days

        left = max(weights)
        right = sum(weights)
        while left < right:
            mid = (left + right) // 2
            if can_ship(mid):
                right = mid
            else:
                left = mid + 1
        return left
```

## Complexity

| Time                   | Space |
| ---------------------- | ----- |
| O(n log(sum(weights))) | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
