
g0301_0400.s0330_patching_array.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0301_0400.s0330_patching_array;
// #Hard #Array #Greedy #2022_03_15_Time_0_ms_(100.00%)_Space_44.1_MB_(38.52%)
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 - 2025 Weber Informatics LLC | Privacy Policy