330 Patching Array
Problem:
Solutions:
public class Solution {
public int minPatches(int[] nums, int n) {
boolean[] has = new boolean[n];
int[] toFill = new int[1];
toFill[0] = n;
for (int i = 0; i < nums.length; i ++) {
if (nums[i] <= n) {
addElement(nums[i], has, nums, toFill, n);
}
}
int start = 0;
int count = 0;
while (toFill[0] != 0) {
while (has[start] == true) {
start ++;
}
addElement(start + 1, has, nums, toFill, n);
count ++;
}
return count;
}
private void addElement(int add, boolean[] has, int[] nums, int[] toFill, int n) {
Queue<Integer> added = new LinkedList<Integer>();
for (int j = 0; j < n; j ++) {
if (has[j] == true && j + add < n && has[j + add] == false) {
added.add(j + add);
}
}
while (!added.isEmpty()) {
has[added.poll()] = true;
toFill[0] --;
}
if (has[add - 1] == false) {
has[add - 1] = true;
toFill[0] --;
}
}
}Previous329 Longest Increasing Path in a MatrixNext331 Verify Preorder Serialization of a Binary Tree
Last updated