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

# Patching Array Python Solution with Tests

> Tested Python solution for LeetCode 330 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 330, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/patching-array/description/).

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

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

## Problem

Given a sorted integer array \<code>nums\</code> and an integer \<code>n\</code>, add/patch elements to the array such that any number in the range \<code>\[1, n]\</code> inclusive can be formed by the sum of some elements in the array.

Return \<em>the minimum number of patches required\</em>.

### Examples

```
Input: nums = [1,3], n = 6
Output: 1
Explanation:
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
So we only need 1 patch.
```

```
Input: nums = [1,5,10], n = 20
Output: 2
Explanation: The two patches can be [2, 4].
```

```
Input: nums = [1,2,2], n = 5
Output: 0
```

### Constraints

* 1 \<= nums.length \<= 1000
* 1 \<= nums\[i] \<= 10^4
* nums is sorted in ascending order.
* 1 \<= n \<= 2^31 - 1

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(nums) + log n)
    # Space: O(1)
    def min_patches(self, nums: list[int], n: int) -> int:
        patches = 0
        miss = 1  # smallest sum in [1, miss) that cannot be formed yet
        i = 0
        while miss <= n:
            if i < len(nums) and nums[i] <= miss:
                miss += nums[i]
                i += 1
            else:
                patches += 1
                miss += miss
        return patches
```

## Complexity

| Time | Space |
| - | - |
| O(len(nums) + log n) | O(1) |

## Tags


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