Тёмный

Search Insert Position - Binary Search - Leetcode 35 - Python 

NeetCode
Подписаться 845 тыс.
Просмотров 64 тыс.
50% 1

Опубликовано:

 

26 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 70   
@NeetCode
@NeetCode 3 года назад
💡 BINARY SEARCH PLAYLIST: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-U8XENwh8Oy8.html
@luckychitundu1070
@luckychitundu1070 2 года назад
Nice One! I came here to find out why we are returning left. Thanks brother.
@zhan1447
@zhan1447 2 года назад
Same here. I am a little confused why we are returning left in the end?
@hasibqureshi6409
@hasibqureshi6409 Год назад
Same here
@ernie2111
@ernie2111 3 года назад
Keep them coming! You’re doing a great job
@ruksharalam173
@ruksharalam173 Год назад
One o' the best LeetCode channels in YT
@pranavkul525
@pranavkul525 2 года назад
Thanks a lot. The video was really helpful and the way you explain it is so great
@alphabee8171
@alphabee8171 2 года назад
unlike others you had a clear understanding why to use binary search.
@lylelll2275
@lylelll2275 3 года назад
like your videos! They are easy to understand.
@skms31
@skms31 2 года назад
Who invents these problems !! Crazy stuff
@duvincent869
@duvincent869 2 года назад
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.
@CS_n00b
@CS_n00b 2 года назад
that breathing at the end freaked me out lol
@NeetCode
@NeetCode 2 года назад
Lol damn I dont know how that was left in, it sounds like it was looping a few times
@huyhoangnguyenhuu2136
@huyhoangnguyenhuu2136 Год назад
great explanation. Very appreciate!!
@thinja
@thinja 2 года назад
wow that trick at the end was *chefs kiss*
@abdullahahmad5838
@abdullahahmad5838 2 года назад
I did something like this: def binary(list1, key): start = 0 end = len(list1) - 1 while start 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
@servantofthelord8147
@servantofthelord8147 4 месяца назад
Did exactly this
@무광-r8k
@무광-r8k 2 года назад
Thank you sir. i am kotlin user but its really impressive thank you
@youtubeDaddy525
@youtubeDaddy525 3 года назад
Well explained !
@Priyam6F4
@Priyam6F4 Год назад
Approach The problem is little bit modified when compared to finding the floor/ceil of an element using binary search. Usually to find the floor,(Here I am assuming you know the basic binary search concept of finding the floor and ceil) we consider the rightmost search space. if(nums[mid]
@_DashingAdi_
@_DashingAdi_ Год назад
Floor of 5 is 4? dafuq
@Priyam6F4
@Priyam6F4 Год назад
@@_DashingAdi_ Intelligent man, by floor I mean, the closest minimum, big brain time
@Oyeah900
@Oyeah900 3 года назад
I am confused about when to use (less than)< vs
@ravivanam7868
@ravivanam7868 2 года назад
left and right pointers define the range in which the target lies. so if left is 3 and right is 12 then the target could be at any index from 3 to 12 including 3 and 12. now imagine what happens when left = right = some number, say 4 then the target could be at any index from 4 to 4 including 4. see it? it means it must be at 4(=left = right).
@HuangweiHenryFang
@HuangweiHenryFang 2 года назад
I am also confused with it. What I understand is that it depends on how you move your boundary. If you don't set the boundary over the mid (e.g left = mid, but not left = mid+1), you should be careful about using
@srinathreddy1727
@srinathreddy1727 Год назад
Very well explained like why we should left, why not right?? Thanks a lot❤
@szymon200000
@szymon200000 3 года назад
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?
@ravivanam7868
@ravivanam7868 2 года назад
waste of space and time cuz the code would still work when target is out of bounds
@_DashingAdi_
@_DashingAdi_ Год назад
​​@@ravivanam7868ardon me if this is a dumb question but how would the code still work if the target is out of bounds, won't the program stop executing after it returns 0?
@xvarmond
@xvarmond Год назад
Thanks you!
@averychen4633
@averychen4633 9 месяцев назад
for me the hardest is not the algorithm, it is the extreme cases such as nums=[1] and the boundaries. I absolutely hate 0 index!
@aliadel1723
@aliadel1723 Год назад
you are the best ♥
@biomed3d503
@biomed3d503 2 года назад
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
@adamgertzkin1715
@adamgertzkin1715 Год назад
The state where L=R is one step before the algo will end. We have: while(L
@mohammadjaffar4461
@mohammadjaffar4461 6 месяцев назад
for a moment i was feeling really dumb, i tried different ways and stuck for about 45 mins, then i saw ur explaination,, i wonder why this logic didn;t hit me ,, im feeling really dumb😓😓
@mageshiz7346
@mageshiz7346 2 года назад
Thank you sir
@RojinSharma-c8l
@RojinSharma-c8l 3 месяца назад
It was really hepful
@canr
@canr 3 года назад
great video. I have one small question though. why do we add or subtract 1 from mid?
@sravanikatasani6502
@sravanikatasani6502 3 года назад
we are updating the range to search here.. if the target is not equal to element at position mid, we can say that it has to lie either on left or right side of mid ,i.e search area will half of the previous. if target is less than mid then we search on left side with right=mid-1 ( we are not considering element at mid because we already checked if target== element at mid) and the other case left=mid+1 if target> element at mid.
@lucasjackson7647
@lucasjackson7647 3 года назад
you should explain that // will only keep the whole number component
@Famelhaut
@Famelhaut Год назад
1 liner: import bisect class Solution: def searchInsert(self, nums: List[int], target: int) -> int: return bisect.bisect_left(nums, target)
@kirich321
@kirich321 2 года назад
is there a recursive function solution?
@DomKeon
@DomKeon 2 года назад
Recursive kotlin solution: fun searchInsert(nums: IntArray, target: Int): Int { return helper(nums, target, 0, nums.size - 1) } private fun helper(nums: IntArray, target: Int, left: Int, right: Int): Int { val middleIndex = (left + right) / 2 if (nums[middleIndex] == target) return middleIndex if (left > right) { return left } return if (target > nums[middleIndex]) { helper(nums, target, middleIndex + 1, right) } else helper(nums, target, left, middleIndex - 1) }
@SeyyedMohammadLoghmanDastgheyb
@SeyyedMohammadLoghmanDastgheyb 2 года назад
Hi 🙂 this solution doesn't work for 👉 nums=[1, 3, 5, 6], target=2 😵‍💫 What am I missing? 🧐
@xingdi986
@xingdi986 3 года назад
in case the target does not exist in the array, can we check the target value with the most left and right value first and then do the binary search?
@ssshukla26
@ssshukla26 3 года назад
that is O(n) operation, so we cannot check.
@ravivanam7868
@ravivanam7868 2 года назад
the time complexity wouldn't change if you did, but it is unnecessary.
@issamuhsen1245
@issamuhsen1245 2 года назад
Why not do this: If target in nums: Return nums.index(target) Else: nums.append(target) nums.sort() Return nums.index(target)
@Saotsu1
@Saotsu1 2 года назад
This is slow as hell, the purpose is a efficient solution
@issamuhsen1245
@issamuhsen1245 2 года назад
@@Saotsu1 on leetcode it says that the runtime is 102 ms and it beat about 46 percent of submissions. It that slow? I heard that leetcode sometimes give random numbers about submissions
@Saotsu1
@Saotsu1 2 года назад
@@issamuhsen1245 Your code will run with O(N), which isn't slow, but very slow compared to O(log n)
@issamuhsen1245
@issamuhsen1245 2 года назад
@@Saotsu1 thanks for help
@rajeshadam978
@rajeshadam978 2 года назад
thanks
@meowmeow21588
@meowmeow21588 Год назад
Not me patching up six conditions like the transformers in Transformers 2 that joined together to barely crawl pass through the finish line and guy just does a regular binary search QAQ
@Dark_engine_er
@Dark_engine_er 14 дней назад
the best
@lamedev1342
@lamedev1342 2 года назад
Cool solution. I only looked at this once i finished my solution. Mine was basically implement binary search, and if it doesnt work, there are 3 cases to take account for. Code: begin = 0 end = len(nums) - 1 last_value = len(nums) - 1 first_index = 0 while begin target: end = midpoint - 1 else: return midpoint for i in range(len(nums)): if target > nums[last_value]: return last_value + 1 if target < nums[first_index]: return 0 if nums[i] < target < nums[i+1]: return i+ 1
@hamoodhabibi7026
@hamoodhabibi7026 2 года назад
left you mad man xD. ur the best
@gunahawk6893
@gunahawk6893 2 года назад
bro please do a dsa course in python
@funfingen
@funfingen Год назад
When I submit this solution, it beats only 47% in runtime... Why do you think that's the case?
@RajeshSingh-zc6ct
@RajeshSingh-zc6ct Год назад
what if we have duplicate values??
@scpcomm1215
@scpcomm1215 Год назад
i would have never come out with this lol. Maybe, i would have studied computer science and all i did was code and learn a gang of techniques and thrive to always come out with new ways to make algorithms ran faster and more efficient...crazy, respect to you mah software engineeris, programmers or whatever you are. honestly , im happy with iteration throught the entire loop lol Doesn't that make more sense haha. jk, ima memorize this technique like people memorize Maradona and Magico Gonzales tricks and then show the world
@SaadQureshiOfficial
@SaadQureshiOfficial Год назад
I don't understand the return L part.
@CST1992
@CST1992 6 месяцев назад
Now they specify that they *require* logarithmic time complexity in the problem itself.
@doc9448
@doc9448 5 месяцев назад
Literally. And it's still an easy.
@CST1992
@CST1992 5 месяцев назад
@@doc9448 Yeah, it still is. Because it's a very basic application of binary search.
@jackbaker995
@jackbaker995 7 месяцев назад
he's breathing so hard at the end lol. It really is a difficult question though, from a beginner like me at least.
@joydeep_
@joydeep_ 2 месяца назад
🤑🤑🤑
@aditi1786
@aditi1786 2 месяца назад
#completedbyaditi2206
@sanooosai
@sanooosai 8 месяцев назад
thank you
Далее
How I would learn Leetcode if I could start over
18:03
Просмотров 632 тыс.
Find Peak Element - Leetcode 162 - Python
11:02
Просмотров 45 тыс.
Learn Python OOP in under 20 Minutes
18:32
Просмотров 80 тыс.
Binary Search - Leetcode 704 - Python
9:40
Просмотров 163 тыс.
My Brain after 569 Leetcode Problems
7:50
Просмотров 2,6 млн
Let's code a beginner's Python BANK PROGRAM 💰
15:01
Просмотров 227 тыс.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Is Computer Science still worth it?
20:08
Просмотров 421 тыс.