1539. Kth Missing Positive Number
use set
class Solution {
public int findKthPositive(int[] arr, int k) {
Set<Integer> set = new HashSet<>();
for (int num : arr) {
set.add(num);
}
int missing = 0;
int max = arr[arr.length - 1];
for (int i = 1; i <= max; i++) {
if (!set.contains(i)) {
missing++;
}
if (missing == k) {
return i;
}
}
return max + (k - missing);
}
}more logical thinking
another one - this is important
binary search

new version
Last updated