Patching Array

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

Return the minimum number of patches required.

Example 1
Inputnums = [1,3], n = 6
Output1
Adding 2 lets the possible sums cover every number in the range [1, 6], so only 1 patch is needed.
Example 2
Inputnums = [1,5,10], n = 20
Output2
The two patches can be [2, 4].

Constraints

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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate