Search in Rotated Sorted Array II - Leetcode 81 - Python

  Рет қаралды 16,991

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 33
@SASA_maxillo
@SASA_maxillo Жыл бұрын
POV: *you are struggling on a leetcode problem* then you found neetcode have solve it the BEST feeling ever
@aadil4236
@aadil4236 Жыл бұрын
I feel much safer doing daily leetcode challenges by your return. Thank you! Suggestion: We would love to see explanations of weekly contest as well. After it ends of course.
@polycrylate
@polycrylate Жыл бұрын
A small improvement: If nums[l] == nums[m] and nums[l] != nums[r] It's guaranteed that the pivot is right of middle i.e. you are on the higher/right part of array Because if the pivot is on the left of middle, it means that middle -> right must all be the same number that loops back to the left, however as middle != right (as left != right and left == middle) this isn't the case So only l += 1 in the case of nums[m] == nums[l] and nums[l] == nums[r], and extend the other case to be
@sumitraj6878
@sumitraj6878 9 ай бұрын
in the case of nums[m] == nums[l] and nums[l] == nums[r] why only l++, do e- - as well. more efficient.. eliminate the same start and end elements because they aint our target. this will help shorten up the search space.
@flp508
@flp508 Ай бұрын
You are a great teacher. I only watched your explanation once and managed to code it all by myself in the first try.
@metarus208
@metarus208 Жыл бұрын
glad to having you post regularly again.
@carsonfreeman6955
@carsonfreeman6955 Жыл бұрын
These explanations are amazing!
@Scarsofevil
@Scarsofevil 7 ай бұрын
If this problem worstcase senario is o(n). Can't we just integrate through the array and return True in the worstcase scenario?
@anuragnandan6155
@anuragnandan6155 Ай бұрын
In your explanation of [2,2,2,2,3,1], if nums[l] == nums[m] wouldn’t that mean everything between l and m is equal and since our target was not at nums[m], instead of l+=1 we can do l=m+1 ? Am I missing something?
@josepadilla4024
@josepadilla4024 9 ай бұрын
He sounded so mad at this problem haha
@lesterdelacruz5088
@lesterdelacruz5088 7 ай бұрын
Not the best problem. Because eliminating left pointer 1 by 1 in the worst case would still be O(n) so it doesn't improve anything if you simply just linearly search.
@panmacabre9895
@panmacabre9895 Жыл бұрын
thank you daddy
@peskovdev
@peskovdev Жыл бұрын
sometimes you can understand is that left or right portion not only by left & mid pointers, but also by mid & right pointers, so you will omit some linear operations. Here is the code: ``` class Solution: def search(self, nums: List[int], target: int) -> bool: lp, rp = 0, len(nums) - 1 while lp nums[rp] or nums[mp] > nums[lp]: # left sorted portion if nums[lp]
@MrLeyt1125
@MrLeyt1125 7 ай бұрын
On #equal you should move both lp and rp
@rhugvedbhojane4387
@rhugvedbhojane4387 Жыл бұрын
Good to see you back buddy.
@aaditya_87
@aaditya_87 7 ай бұрын
2:08 which day? which problem
@michelle_tsai_drums
@michelle_tsai_drums Жыл бұрын
Thorough explanation as always!
@uptwist2260
@uptwist2260 Жыл бұрын
Thanks for the daily
@ievgen6209
@ievgen6209 7 ай бұрын
imo, a little bit overcomplicated... the only case we need to handle in N time - skip duplicates if nums[0] == nums[-1] otherwise it is original solution: l = 0 while l < (len(nums) - 1) and nums[l] == nums[-1]: l += 1 # Code for Search in Rotated Sorted Array I problem Beats 86.73% of users with Python3 right or I'm missing something?
@MrLeyt1125
@MrLeyt1125 7 ай бұрын
Why not move both left and right pointers?
@floatingpoint7629
@floatingpoint7629 Жыл бұрын
the question mentions to decrease the overall operation steps. how does this algo do that?
@polycrylate
@polycrylate Жыл бұрын
It doesn't force a linear search, it always tries to do a binary until it's stuck and removes elements 1 by 1 until it can again What they meant by that line I think is even tho worst case is O(n) they wanted a better AVG case
@floatingpoint7629
@floatingpoint7629 Жыл бұрын
@@polycrylate got it, thanks
@flamendless
@flamendless Жыл бұрын
Would "target in numbers" in Python work?
@NeetCodeIO
@NeetCodeIO Жыл бұрын
That is basically a linear scan, it may get accepted but i think it's not the intended solution.
@MP-ny3ep
@MP-ny3ep Жыл бұрын
Great explanation as always . Thank you
@uttamkumarreddygaggenapall2504
@uttamkumarreddygaggenapall2504 Жыл бұрын
Thank You
@aniruddhachaki7165
@aniruddhachaki7165 Жыл бұрын
Wouldn't it be easier if we just sort the input first and then apply traditional Binary Search? in this problem, we don't need the target's index anyway. It works in this case where we just need to enter true or false.
@panmacabre9895
@panmacabre9895 Жыл бұрын
sorting would take O(nlogn)
@aniruddhachaki7165
@aniruddhachaki7165 Жыл бұрын
@@panmacabre9895 Yeah. That might be the issue. So, the above solution is the best, if we have to return index, we can with just do a small change
@sumitsharma6738
@sumitsharma6738 Жыл бұрын
the only catch in this problem is when you don't know which part is sorted so you just compare mid value with s and e and if (s and mid) are equal then s++ or if (mid or e) are equal then e--
@SASA_maxillo
@SASA_maxillo Жыл бұрын
why not just doing: return target in nums EASYYYYYYYYY
@ashokbabug40
@ashokbabug40 Жыл бұрын
Worst explanation
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 300 М.
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,6 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 5 МЛН
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 192 М.
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
12:19
Rotating the Box - Leetcode 1861 - Python
15:14
NeetCodeIO
Рет қаралды 6 М.
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 291 М.
BS-5. Search Element in Rotated Sorted Array II
12:44
take U forward
Рет қаралды 190 М.
BS-8. Single Element in Sorted Array
22:16
take U forward
Рет қаралды 172 М.
Search in rotated sorted array - Leetcode 33 - Python
13:28
NeetCode
Рет қаралды 362 М.