LeetCode Day 19 - Search in Rotated Sorted Array

  Рет қаралды 40,456

Errichto Algorithms

Errichto Algorithms

Күн бұрын

Пікірлер: 71
@mariansoltan1318
@mariansoltan1318 4 жыл бұрын
When I saw this problem, first thing that i remembered was you binary search lecture where you talked about that.
@AnkurDeka
@AnkurDeka 4 жыл бұрын
Totally loved your solution. I think the key idea was to understand that within the rotated array, by comparing any element with the first element we can determine which section the element belongs to. We could instead compare with the last element as well.
@jamjam3448
@jamjam3448 4 ай бұрын
Thanks so much. I finally understand it after several hours of studying
@domdom_hello
@domdom_hello 4 жыл бұрын
Dude, you're the best! You help me a lot! I just love your style to explain things - so easy for me to understand, so clear. Thanks man!
@vineethkumar6710
@vineethkumar6710 3 жыл бұрын
@Errichto I watched your 'Binary Search Tutorial' and I could easily come up with optimal solution for this question. Thanks for your amazing videos
@mohamadalibaydoun6418
@mohamadalibaydoun6418 4 жыл бұрын
This was one of my Google interview questions 5 years ago. Aced it, got in
@miteshkumar3183
@miteshkumar3183 4 жыл бұрын
I still cant get it consistently. The first time I came up with it myself but I always mess up when I re attempt the problem, and have to submit several times. Right now I am passing 150 /200 test cases on leetcode, gg.
@fauzanwaseem6436
@fauzanwaseem6436 4 жыл бұрын
@@miteshkumar3183 i went for a linear search just to feel better lol
@PiyushKumar-qj8ue
@PiyushKumar-qj8ue 4 жыл бұрын
I used the first approach to solve it but I'm very pleased to learn another approach. Thanks ❤️
@SurajKumar-bw9oi
@SurajKumar-bw9oi 4 жыл бұрын
I ran both codes Amazingly, both approach gives the same running time of 4 ms in Leetcode.
@Rekefa
@Rekefa 4 жыл бұрын
Can you do a data structures/ algorithm crash course? Would be a pleasure to learn from you
@genuineprofile6400
@genuineprofile6400 4 жыл бұрын
He may not be good with just theory.
@rentib9136
@rentib9136 4 жыл бұрын
@@genuineprofile6400 He is ultra good
@CarrotCakeMake
@CarrotCakeMake 4 жыл бұрын
Wow, what a thing to see. I was just implementing a BTree data structure using circular buffers for children and data. An IRL version of this problem. Except I guess I know where the start of the circle is.
@shreyasshastry5795
@shreyasshastry5795 4 жыл бұрын
Code Jam 1B video???
@hellowill
@hellowill 4 жыл бұрын
Very cool! I think of it like the rotation partitions the array into two sorted parts. E.g. [3,4,5,0,1,2] = [3,4,5] ++ [0,1,2]. So if you're in the same part as target you're allowed to do standard binary search and if you're not in the same part then simply search that part with target in it.
@RMGringo2122
@RMGringo2122 4 жыл бұрын
William you still need to find the point where one array splits from another though right? Which would be slower. Unless I misunderstood your solution
@hellowill
@hellowill 4 жыл бұрын
@@RMGringo2122 You do that with the first element comparison (its the same code as in the video)
@mohamed4441
@mohamed4441 4 жыл бұрын
good work! please keep doing these videos.
@usuyus
@usuyus 4 жыл бұрын
I want to ask a question: because the question said that there wouldn't be any duplicates in the list, we didn't really consider examples like this: [1,1,2,4,0,1,1,1,1,1,1] 4 In this case, we say m = 5 in the first iteration and encounter a 1 in nums[5]. Because we assume no duplicates, we think m is in the first sector while it actually is in the second one. This would cause the algorithm to not work. I have been thinking about the general case where duplicates are allowed and where we have to be able to also answer test cases like this, but couldn't actually find a feasible solution / modification to the existing solution. Any help is welcome as a comment :) (I have been looking for a solution for too long now... I really need help)
@kabaran2
@kabaran2 4 жыл бұрын
Unfortunately there is no solution to do this with O(log(n)). At least not in the worst case. Consider the array [1,1,2,1,1,1]: there is no way to find the "2" with a binary search because none of the other elements give you any clue as to were the "2" is (or if there even is one). So you will end up looking at every item on the list which means O(n). However, if the first item and the last item are not the same, then a binary search can reveal the location of the "shift". So let's say we have something like [3,3,4,4,4,5,1,1,2,2]. In this elements all the elements up to a certain index are >=3, and all the elements beyond that index are
@usuyus
@usuyus 4 жыл бұрын
@@kabaran2 Thanks for the reply. I did think of the O(m + logn) solution; I was just wondering whether an O(logn) algorithm would exist or not.
@AshishSingh-dn8wb
@AshishSingh-dn8wb 4 жыл бұрын
Amazing explanation! Can you upload a video on Code Jam Round 1B maybe?
@iamtrash288
@iamtrash288 4 жыл бұрын
very nice explanation, loved it!
@dr.darkfurygaming9174
@dr.darkfurygaming9174 4 жыл бұрын
It also accepted O(n)
@Sebastian-eb2ux
@Sebastian-eb2ux 4 жыл бұрын
Maybe, but the problem explicitly states that your solution has to have a complexity of O(log n)
@dr.darkfurygaming9174
@dr.darkfurygaming9174 4 жыл бұрын
@@Sebastian-eb2ux Ya i know i just try and it accepted
@aminel2a
@aminel2a 4 жыл бұрын
I can't understand why people react by dislike 😂BigUp anyway
@venkyvirat92
@venkyvirat92 6 күн бұрын
bro.....according to ur template u haven'nt used separate if to check if arr[mid]==target but why u are using here???
@jpfr012
@jpfr012 4 жыл бұрын
since I'm lazy, I just used the vector::iterator java users can also use the array.indexOf(target)
@BarkaDog
@BarkaDog 4 жыл бұрын
That will be O(n)
@jpfr012
@jpfr012 4 жыл бұрын
@@BarkaDog thanks, I didn't know that. but why didn't give TLE?
@neer3
@neer3 4 жыл бұрын
Hey Errichto please tell the fine diff between (low+high)/2 and (low +(high-low)/2).
@yashsanjeev8480
@yashsanjeev8480 4 жыл бұрын
low+high may overflow resulting in garbage values of mid
@mohitranka9840
@mohitranka9840 4 жыл бұрын
In statically typed languages such as C++, which have fixed range for int, (low+high) may result in overflow, if the numbers are big. The other variant of high-low prevents it, atleast when the numbers are non negative.
@vibhor3049
@vibhor3049 4 жыл бұрын
> suppose you have a very large (like v v large around consuming 30 bits) values of high and low. Then (low+high) would give a value which simply can't be stored in int data type, hence an overflow error. > (high+low)/2 doesn't work with pointers- type error
@ivanp4740
@ivanp4740 4 жыл бұрын
i found this video with solving leetcode problems very useful. I hope errichto will do that even after leetcode challenge I did this problem twice. And twice i wasn't able to this without any mistakes(always fail on edge cases) But that explanation and code organization helps me to really understand how it could be simplified. Because my thoughts(and solution) was overcomplicated with a lot of "if else"
@codeencode5728
@codeencode5728 4 жыл бұрын
For the cases like this Eg: [5,4,3,1,2] . can the time complexity can log n instead of 3*logn ? Thanx Errichto
@NotFound-hy7qb
@NotFound-hy7qb 4 жыл бұрын
Video is right on time
@almuhimen8023
@almuhimen8023 4 жыл бұрын
Why not linear search?
@akhilanand3708
@akhilanand3708 4 жыл бұрын
code jam round 1 B solution please
@arpansingh4284
@arpansingh4284 4 жыл бұрын
Please do a video on code jam 1B
@sakshamsethi4123
@sakshamsethi4123 4 жыл бұрын
Codejam video please !
@thehalfblo0dprince
@thehalfblo0dprince 4 жыл бұрын
why this problem passes in O(n) complexity?
@flintpiratehunter8723
@flintpiratehunter8723 4 жыл бұрын
I think it is because it is just for interview preparation. You do not win any price; O(log n) is just for making the problem more interesting.
@ashishdukare1313
@ashishdukare1313 4 жыл бұрын
[India] Superb solution. thanks for the video : )
@rajdave9862
@rajdave9862 4 жыл бұрын
What is difference between low+high/2. And low +(high-low)/2 ? Please tell
@flintpiratehunter8723
@flintpiratehunter8723 4 жыл бұрын
Low + high may overflow
@charchitsharma8902
@charchitsharma8902 4 жыл бұрын
Implementing this in python givens TLE.
@dhananjaysonawane1996
@dhananjaysonawane1996 4 жыл бұрын
I saw other problems on leetcode, codeforces which gave TLE on python but AC on cpp :( Maybe time limit is not set properly for python.
@charchitsharma8902
@charchitsharma8902 4 жыл бұрын
@@dhananjaysonawane1996 Yes :(
@flintpiratehunter8723
@flintpiratehunter8723 4 жыл бұрын
Nah, I got AC in Python with similar code
@dhananjaysonawane1996
@dhananjaysonawane1996 4 жыл бұрын
@@flintpiratehunter8723 They later change time limits. leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/discuss/586547/C++-Top-Down-DP/509109
@charchitsharma8902
@charchitsharma8902 4 жыл бұрын
@@dhananjaysonawane1996 Yes!
@thepodfunnel
@thepodfunnel 3 жыл бұрын
He is Mini Lex Fridman
@tetrreter6548
@tetrreter6548 4 жыл бұрын
Way not use function build in like find and count?
@rentib9136
@rentib9136 4 жыл бұрын
for vector they are linear
@sahilsharma2952
@sahilsharma2952 4 жыл бұрын
Google codejam solutions?
@danilsabirov715
@danilsabirov715 4 жыл бұрын
Errichto, can you describe that the mid = (lo + hi)/2 is different than mid = lo + (hi - lo) /2? I can't understand what is the difference that you mentioned
@alexandrugabriel9202
@alexandrugabriel9202 4 жыл бұрын
both do the same thing, but the second is better because you avoid overflow problems if lo and hi are big.
@sameerchouragade2706
@sameerchouragade2706 4 жыл бұрын
If low and high are very big , high + low becomes even more big , which could lead to overflow of int
@vaibhavvashist238
@vaibhavvashist238 4 жыл бұрын
sir who to get motivated and consistency in cp?
@fiziblank2609
@fiziblank2609 4 жыл бұрын
Sir, which keyboard do you use?
@leetcodesolver9092
@leetcodesolver9092 4 жыл бұрын
I also solved it using similar approach.
@zeyneperol2984
@zeyneperol2984 4 жыл бұрын
Thanks!
@abilashshankarpalli9301
@abilashshankarpalli9301 4 жыл бұрын
Hi Errichto, Love From India I am beginner in competative programming, and I am struggling to figure out how to take stdin and stdout for the programs here in "codeforces" so I can practice. I am using Python as language. Would you please help me with by making a video on how to take stdin and stdout in python. Or atleast please reply me with the code in python.
@philtoa334
@philtoa334 4 жыл бұрын
Thx
@inosuke44
@inosuke44 4 жыл бұрын
first viewer?
@ДмитрийВихляев-к9п
@ДмитрийВихляев-к9п 4 жыл бұрын
Please use zoom
LeetCode Day 20 - Binary Search Tree from Preorder Traversal
19:08
Errichto Algorithms
Рет қаралды 28 М.
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 315 М.
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
Search in Rotated Sorted Array II - Leetcode 81 - Python
17:36
NeetCodeIO
Рет қаралды 18 М.
How many times is a sorted array rotated?
13:03
mycodeschool
Рет қаралды 172 М.
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 208 М.
Search in rotated sorted array - Leetcode 33 - Python
13:28
NeetCode
Рет қаралды 377 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 152 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 192 М.
Search in Rotated Sorted Array | LeetCode problem 33
11:35
Technosage
Рет қаралды 6 М.