330. Patching Array

题目链接:https://leetcode.com/problems...

想了半天没想出来,参考discussion里的解法:
https://discuss.leetcode.com/...

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

lulouch13
13 声望6 粉丝