File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed
1646-kth-missing-positive-number Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change 11# Approach 2: Binary Search
22
3- # Time: O(log N )
3+ # Time: O(log n )
44# Space: O(1)
55
66class 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+
You can’t perform that action at this time.
0 commit comments