Skip to content

Commit 6fca276

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.9 MB (31.77%)
1 parent c5ff8a4 commit 6fca276

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
# Approach 2: Binary Search
22

3-
# Time: O(log N)
3+
# Time: O(log n)
44
# Space: O(1)
55

66
class Solution:
77
def findKthPositive(self, arr: List[int], k: int) -> int:
88
left, right = 0, len(arr) - 1
9-
109
while left <= right:
1110
pivot = (left + right) // 2
1211

12+
# If number of positive integers which are missing before arr[pivot] is less than k -->
13+
# continue to search on the right.
1314
if arr[pivot] - pivot - 1 < k:
1415
left = pivot + 1
1516
else:
1617
right = pivot - 1
1718

19+
# At the end of the loop, left = right + 1,
20+
# and the kth missing is in-between arr[right] and arr[left].
21+
# The number of integers missing before arr[right] is
22+
# arr[right] - right - 1 -->
23+
# the number to return is
24+
# arr[right] + k - (arr[right] - right - 1) = k + left
1825
return left + k
26+

0 commit comments

Comments
 (0)