132. Patching Array

HardArrayArray

Given a sorted array nums and integer n, return the minimum number of patches (elements to add) so that every integer in [1,n] can be formed as a sum of some subset. Input: '[nums], n'.

Input: '[nums], n'.

Output: Integer — minimum patches.

Examples

Example 1
Input: [1,3], 6
Output: 1
Explanation: Add 2 to cover all of [1,6].
Example 2
Input: [1,5,10], 20
Output: 2
Explanation: Two patches needed.
Example 3
Input: [1,2,2], 5
Output: 0
Explanation: Already covers [1,5].

Constraints

Asked by

GoogleMetaAmazon
Solve this problem in the editor →