Search Insert Position - Leetcode 35 - Binary Search (Python)

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

we could just return L if target is not found since L will follow the pos of the target anyway so:
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
L, R = 0, len(nums) - 1

while L <= R:
mid = L + (R - L) // 2

if nums[mid] == target:
return mid
elif nums[mid] > target:
R = mid - 1
else:
L = mid + 1

return L

You are doing amazing job greg you have been a great resource for me so far I have started leetcode 1 month ago and your videos and solutions including Algomap have been very useful, thank you so much.

tesfamichael
Автор

It would be good, if you 'return l' istead of 'return m'.

nerdyboy
Автор

this is not the correct solution ig, getting 44 test cases right

Noobshit