-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_insert_position.py
38 lines (36 loc) · 1.17 KB
/
search_insert_position.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution:
"""
Task:
Given a sorted array of distinct integers and a target value,
return the index if the target is found.
If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
"""
def searchInsert(self, nums: list[int], target: int) -> int:
if target < min(nums):
return 0
elif target > max(nums):
return nums.index(max(nums)) + 1
elif target in nums:
return nums.index(target)
else:
i = 0
while i < len(nums):
if nums[i] < target < nums[i + 1]:
return i+1
i += 1
# Using binary search algorithm, time complexity O(Log(n))
def searchInsert(self, nums: list[int], target: int) -> int:
n = len(nums) - 1
l, r = 0, n
if target < nums[l]:
return 0
while l <= r:
mid = l + (r-l)//2
if nums[mid] == target:
return mid
elif nums[mid] < target:
l = mid+1
else:
r = mid-1
return l