RANDOM PICK INDEX | LEETCODE # 398 | PYTHON RESERVOIR SAMPLING

  Рет қаралды 9,344

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер: 31
@smoran02
@smoran02 10 ай бұрын
Really missing the proof for why it works for the earlier indices. For example for the second value, probability of 1st selection is 1/2. Probability of being selected next is 1/2 * 2/3 = 1/3. Probability of being selected when you have seen 4 values is 1/2*2/3*3/4 = 1/4. This is what makes the explanation make sense
@provarence7361
@provarence7361 3 ай бұрын
great comment, was confused about this
@manojamrutharaj9071
@manojamrutharaj9071 2 жыл бұрын
Thank you for all these videos with easy explanations. Will be glad if we have all Hard Leetcode problems playsist.
@kareni7572
@kareni7572 7 ай бұрын
Found this solution and explanation more intuitive than others!
@shuier525
@shuier525 2 ай бұрын
Comment from Nov 2024. Thank you for pointing out this great solution. The solution tab for this question has been disabled. This RESERVOIR sampling solution is still TLE. Without your explanation, I do not even know there is a RESERVOIR SAMPLING solution. I think the main reason is because leetcode might updated the constraint to a larger number: Constraints: 1
@shoaibakhtar9194
@shoaibakhtar9194 7 ай бұрын
Equal possibility return explanation. So in the loop, the index closer to the start is easier to be picked, but also easier to be replaced since more iterations left behind. For example, let's say we have 5 duplicates in total. For the 2nd index, the chance it get returned is: 1/2(pick)*2/3(replace)*3/4(replace)*4/5(replace) = 1/5 with 3 iterations left; Replace probablility is calculated like this:- 2/3(replace means 2 index 1st and 3rd) can be picked and total possibilities are 1,2 and 3 indexes. So change of getting replaced is 2/3) For the 5th index, the chance it get returned is: 1/5(pick) = 1/5 , no chance to be replaced since no iterations left.
@sourvad
@sourvad 2 ай бұрын
You sir, are goated
@KiKi-lt4pf
@KiKi-lt4pf Жыл бұрын
Awesomely explained🎉, but when you suddenly moved to coding my eyes got burnt because of the light theme😂
@crackfaang
@crackfaang Жыл бұрын
Normally I hate light mode but I've done so much Leetcode over the years before they had dark mode that my brain is used to the standard theme. But yes, at work whenever I see someone's VS Code and they aren't using a dark theme I think they're a bit nuts
@piyush10793
@piyush10793 Жыл бұрын
This would be biased since we always have higher probability to select the first index that equals target right?
@JAIMEAYMERICHFANS
@JAIMEAYMERICHFANS Жыл бұрын
Not really. On the 2nd pass you have a 50 /50 chance to select the 1st and the same for the 2nd.
@subhadipnandi294
@subhadipnandi294 3 ай бұрын
Very intuitive. Thank you.
@yashmundada2483
@yashmundada2483 29 күн бұрын
so this is even slower than the hashmap?
@400racr
@400racr 15 күн бұрын
i know right i feel like im taking crazy pills how is an o1 time and O(N) space LESS preferred than an o1 space O(N) time??
@YT.Nikolay
@YT.Nikolay 2 жыл бұрын
I solved this question using dict, I am wondering if the interviewer would like to have me implement reservoir sampling. P.S. first time I see this algorithm, worth memorizing? And again, thanks for the video! :)
@YT.Nikolay
@YT.Nikolay 2 жыл бұрын
I am dumb, again, I solved the problem myself and then watched the explanation. #facepalm I guess it clicked, and I would be able to solve it if ever have this on interview
@crackfaang
@crackfaang 2 жыл бұрын
I’d say know the reservoir sampling solution. It’s quite simple and intuitive once you have seen it before. The dictionary one is too simple and if they are asking this question they probably want to see the optimal one
@400racr
@400racr 15 күн бұрын
@@crackfaang how is an O(N) time solution optimal over an O(1) solution
@johnlabarge
@johnlabarge 8 ай бұрын
I'm confused are the numbers sorted?
@SeanKearney-g7d
@SeanKearney-g7d 4 ай бұрын
no
@subee128
@subee128 Жыл бұрын
Thank you very much
@SeanKearney-g7d
@SeanKearney-g7d 4 ай бұрын
reservoir sampling is cool
@Krankschwester
@Krankschwester 8 ай бұрын
Why is this, O(N) solution, "optimal", when you can use a hashmap and answer queries in O(1)? Makes no sense.
@erice.3892
@erice.3892 3 ай бұрын
you have to iterate through the list to make the hashmap, which would be O(N) as well. but i see what you mean. you can store the hashmap and reuse it, therefore save time if this function is called multiple times
@arjityadav8371
@arjityadav8371 2 ай бұрын
Yes, his pick function should be optimized to retrieve in O(1) and that can only be done if he has stored the input in hashmap taking up space O(N)
@kumarlakshya4407
@kumarlakshya4407 Жыл бұрын
great explanation
@annlaosun6260
@annlaosun6260 7 ай бұрын
Your answer did not go through. The hash map method went through fine. I got this "time limit exceed' error using your answer. Please help....
@ayushmusic3684
@ayushmusic3684 3 ай бұрын
me too
@omaro9627
@omaro9627 Жыл бұрын
nice
@GoNguyen-i3k
@GoNguyen-i3k Жыл бұрын
I don't understand the question, can anybody explain that? 😂
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
INSERT INTO CIRCULAR LINKED LIST | LEETCODE # 708 | PYTHON SOLUTION
15:25
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 228 М.
EXCLUSIVE TIME OF FUNCTIONS | LEETCODE 636 | PYTHON SOLUTION
15:42
Cracking FAANG
Рет қаралды 7 М.
How To Predict Random Numbers Generated By Computers
13:54
PwnFunction
Рет қаралды 568 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 691 М.
The Black Box Method: How to Learn Hard Concepts Quickly
14:09
Colin Galen
Рет қаралды 1,1 МЛН
Why do colliding blocks compute pi?
15:16
3Blue1Brown
Рет қаралды 7 МЛН
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 246 М.