-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0035 [E] Search Insert Position
Code with Senpai edited this page Oct 18, 2020
·
1 revision
return the index if target is found, else return where it will be inserted
=> find the smallest/first index that is greater than target
=> leftmost
class Solution:
def searchInsert(self, A: List[int], x: int) -> int:
n = len(A)
l = 0
r = n - 1
while l <= r:
m = (l + r) // 2
if A[m] >= x:
r = m - 1
else:
l = m + 1
return l
footer