Share my greedy solution by Java with simple explanation (time: 1 ms)
public static int minPatches(int[] nums, int n) {\n\t\tlong max = 0;\n\t\tint cnt = 0;\n\t\tfor (int i = 0; max < n;) {\n\t\t\tif (i >= nums.length || max < num
    public static int minPatches(int[] nums, int n) {\n\t\tlong max = 0;\n\t\tint cnt = 0;\n\t\tfor (int i = 0; max < n;) {\n\t\t\tif (i >= nums.length || max < nums[i] - 1) {\n\t\t\t\tmax += max + 1;\n\t\t\t\tcnt++;\n\t\t\t} else {\n\t\t\t\tmax += nums[i];\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn cnt;\n\t}\n\nThe variable `max` records the maximal value that can be formed by the elements in `nums` and patched numbers. If `max` is less than `nums[i] - 1` which means we need to patch a new number, we then patch `max + 1`.
[]