LeetCode – Patching Array (Java)

Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in 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:
nums = [1, 3], n = 6
Return 1.

Analysis

Let miss be the smallest number that can not be formed by the sum of elements in the array. All elements in [0, miss) can be formed. The miss value starts with 1. If the next number nums[i]<=miss, then the boundary is increased to be [0, miss+nums[i]), because all numbers between the boundaries can be formed; if next number nums[i]>miss, that means there is a gap and we need to insert a number, inserting miss itself is a the choice because its boundary doubles and cover every number between the boundaries [0, miss+miss).

Here is an example.
Given nums=[1, 4, 10] and n=50.

miss=1;
i=0, nums[i]<=miss, then miss=1+1=2
i=1, nums[i]>2, then miss = miss+miss = 4
i=1, nums[i]<=miss, then miss = miss+num[i] = 8
i=2, nums[i]>miss, then miss = miss+miss = 16
i=2, nums[i]>miss, then miss = miss+miss = 32
i=2, nums[i]>miss, then miss = miss+miss = 64 
64 > 50. Done! 4 elements are added!

Java Solution

Writing the code is trivial, once we understand the algorithm.

public int minPatches(int[] nums, int n) {
    long miss = 1;
    int count = 0;
    int i = 0;
 
    while(miss <= n){
        if(i<nums.length && nums[i] <= miss){
            miss = miss + nums[i];
            i++;
        }else{
            miss += miss;
            count++;
        }
    }
 
    return count;
}

5 thoughts on “LeetCode – Patching Array (Java)”

  1. This solution is incorrect/sub-optimal. For the given example, only 3 patches are required:

    [1, 2, 4, 8, 10, 26] gets the job done.

  2. can you prove this greedy algo get THE SOLUTION
    nums = [99,100] , n =50
    there is no guarantee that n is bigger than the elements in array
    for any p,q nums[p]≤ n ≤ nums[q], elements sum from 1 to p should be <=n
    so for above case , 10*11/2 = 55 and we simply eliminate 10 -1 = 9 unless missing is part is smaller than 3

Leave a Comment