Class Solution

java.lang.Object
g0301_0400.s0330_patching_array.Solution

public class Solution extends Object
330 - Patching Array.<p>Hard</p> <p>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.</p> <p>Return <em>the minimum number of patches required</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,3], n = 6</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <pre><code> 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. </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,5,10], n = 20</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> The two patches can be [2, 4].</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,2,2], n = 5</p> <p><strong>Output:</strong> 0</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 1000</code></li> <li><code>1 <= nums[i] <= 10<sup>4</sup></code></li> <li><code>nums</code> is sorted in <strong>ascending order</strong>.</li> <li><code>1 <= n <= 2<sup>31</sup> - 1</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minPatches

      public int minPatches(int[] nums, int n)