All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0301_0400.s0330_patching_array.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g0301_0400.s0330_patching_array;

// #Hard #Array #Greedy #2022_07_09_Time_1_ms_(60.00%)_Space_44.3_MB_(27.06%)

public class Solution {
    public int minPatches(int[] nums, int n) {
        int res = 0;
        long sum = 0;
        int i = 0;
        while (sum < n) {
            // required number
            long req = sum + 1;
            if (i < nums.length && nums[i] <= req) {
                sum += nums[i++];
            } else {
                sum += req;
                res++;
            }
        }
        return res;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy