Search Insert Position - Binary Search - Leetcode 35 - Python

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


0:00 - Concept
8:35 - Coding Solution

Leetcode 35
#python #leetcode #neetcode
Рекомендации по теме
Комментарии
Автор

Nice One! I came here to find out why we are returning left. Thanks brother.

luckychitundu
Автор

that breathing at the end freaked me out lol

CS_nb
Автор

Keep them coming! You’re doing a great job

ernie
Автор

unlike others you had a clear understanding why to use binary search.

alphabee
Автор

Thanks a lot. The video was really helpful and the way you explain it is so great

pranavkul
Автор

Imagine array as people line up and facing a wall, whenever a person want to join the queue, we can either choose a person, push him back and sit in front of him, or join at the end of the queue.

The natural of array list cause that, we are searching right (I am bigger than you, then I join behind you so i + 1), if we are searching left (I am smaller than you, I don't have to move but push you behind as i).

That is why we can always choose the lower bound pointer. At first I am thinking why god create left and right bound unequally.

duvincent
Автор

I am confused about when to use (less than)< vs <=(less than or equal to). What is your approach to knowing when to use which one?

Oyeah
Автор

One o' the best LeetCode channels in YT

ruksharalam
Автор

like your videos! They are easy to understand.

lylelll
Автор

Who invents these problems !! Crazy stuff

skms
Автор

Thank you sir. i am kotlin user but its really impressive thank you

무광-rk
Автор

wow that trick at the end was *chefs kiss*

thinja
Автор

at 12:50, I am still not clear why left becomes 1 since the shift has not occured, so left should remains at 0. Any help with better explantion? thnks

biomedd
Автор

I did something like this:

def binary(list1, key):
start = 0
end = len(list1) - 1
while start <= end:
mid = (start + end) // 2
if list1[mid] == key:
return mid
elif key > list1[mid]:
start = mid + 1
elif key < list1[mid]:
end = mid - 1
# print(mid)
if key > list1[mid]:
return mid + 1
elif key < list1[mid] and mid > 0:
return mid
else:
return 0

abdullahahmad
Автор

is there a recursive function solution?

kirich
Автор

one of the things I noticed when solving this problem is that if the target is less than nums[0] you should return 0, and if the target is greater than nums[len(nums)-1], to return len(nums). do you think this is efficient or just a waste of space/time?

szymon
Автор

great video. I have one small question though. why do we add or subtract 1 from mid?

canr
Автор

Very well explained like why we should left, why not right??
Thanks a lot❤

srinathreddy
Автор

1 liner:
import bisect

class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
return bisect.bisect_left(nums, target)

Famelhaut
Автор

Hi 🙂 this solution doesn't work for 👉 nums=[1, 3, 5, 6], target=2 😵‍💫 What am I missing? 🧐

SeyyedMohammadLoghmanDastgheyb