1539. Kth Missing Positive Number
use set
T : O(n)
S: O(n)
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
T : O(n)
S: O(1)
another one - this is important
因為 no missing 時, arr 的值 , 和 idx 差1
binary search
T: O(logn)
S: O(1)

new version
Last updated
Was this helpful?