-
Notifications
You must be signed in to change notification settings - Fork 43
/
search-insert-position.py
139 lines (117 loc) · 3.33 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
35. Search Insert Position
Easy
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.
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
Example 4:
Input: nums = [1,3,5,6], target = 0
Output: 0
Example 5:
Input: nums = [1], target = 0
Output: 0
Constraints:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums contains distinct values sorted in ascending order.
-104 <= target <= 104
"""
# V0
# IDEA : BINARY SEARCH
class Solution:
def searchInsert(self, nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] >= target:
right = mid - 1
else:
left = mid + 1
return left
# V0'
# IDEA : BINARY SEARCH + OTHER CASES HANDLING
class Solution(object):
def searchInsert(self, nums, target):
l = 0
r = len(nums) -1
mid = 0
### NOTE : in normal BINARY SEARCH, we can set r > l or r >= l, while following condtions will be different as well
while r >= l:
mid = l + (r-l) // 2
if nums[mid] == target:
return mid
elif nums[mid] > target:
r = mid -1
elif nums[mid] < target:
l = mid + 1
# init result
result = 0
### NOTE : other cases handling here, 2 cases
# case 1 : r < t < l
# case 2 : t < r < l
if r < l:
# case 1 : r < t < l
if nums[r] < target:
result = r + 1
# case 2 : t < r < l
elif target < nums[r]:
result = r - 1
# edge case : if result < 0, we result 0
return result if result > 0 else 0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/70738108
# via python intrinsic func
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
return bisect.bisect_left(nums, target)
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/70738108
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
N = len(nums)
left, right = 0, N #[left, right)
while left < right:
mid = left + (right - left) / 2
if nums[mid] == target:
return mid
elif nums[mid] > target:
right = mid
else:
left = mid + 1
return left
# V2
# Time: O(logn)
# Space: O(1)
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) / 2
if nums[mid] >= target:
right = mid - 1
else:
left = mid + 1
return left