Skip to content

Commit 16a6dc5

Browse files
daily question added
1 parent 42fc750 commit 16a6dc5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
__pycache__/
33
.DS_Store
44
14. Questions/code.py
5-
14. Questions/temp.py
5+
14. Questions/temp.py
6+
14. Questions/tempCodeRunnerFile.py
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
3+
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.
4+
5+
You must write an algorithm with O(log n) runtime complexity.
6+
7+
Question: https://leetcode.com/problems/search-insert-position/
8+
9+
"""
10+
11+
class Solution:
12+
def searchInsert(self, nums: List[int], target: int) -> int:
13+
l = 0
14+
r = len(nums)-1
15+
16+
while l <= r:
17+
mid = (l+r)//2
18+
19+
if target == nums[mid]:
20+
return mid
21+
22+
if target > nums[mid]:
23+
l = mid +1
24+
25+
else:
26+
r = mid - 1
27+
28+
return l

0 commit comments

Comments
 (0)