> ## 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.

# How Many Apples Can You Put into the Basket

> Tested Python solution for LeetCode 1196 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1196, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket/description/).

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

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

## Problem

You have some apples and a basket that can carry up to `5000` units of weight.

Given an integer array `weight` where `weight[i]` is the weight of the `i-th` apple, return the maximum number of apples you can put in the basket.

### Examples

```
Input: weight = [100,200,150,1000]
Output: 4
Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.
```

```
Input: weight = [900,950,800,1000,700,800]
Output: 5
Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.
```

### Constraints

* `1 <= weight.length <= 10^3`
* `1 <= weight[i] <= 10^3`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n) for the sorted copy
    def max_number_of_apples(self, weight: list[int]) -> int:
        total = 0
        for count, w in enumerate(sorted(weight)):
            total += w
            if total > 5000:
                return count
        return len(weight)
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) for the sorted copy |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.