1539. Kth Missing Positive Number
class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
arr = set(arr)
num = 0
while k:
num += 1
if num not in arr:
k -= 1
return num
O(N)
O(N)
class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] - mid <= k:
left = mid + 1
else:
right = mid - 1
return left + k
O(logN)
O(1)