public int minPatches(int[] nums, int n) {
        long curN=1;
        int curIndex=0;
        int result=0;
        while (curN<=n){
            if(curIndex<nums.length && nums[curIndex]<=curN){
                curN=curN+nums[curIndex];
                curIndex++;
            }else{
                result++;
                curN=curN*2;
            }
        }
        return result;
    }