RANDOM PICK INDEX | LEETCODE # 398 | PYTHON RESERVOIR SAMPLING

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ก.ย. 2024

ความคิดเห็น • 20

  • @smoran02
    @smoran02 5 หลายเดือนก่อน +14

    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

  • @annlaosun6260
    @annlaosun6260 3 หลายเดือนก่อน +1

    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....

  • @SeanKearney-g7d
    @SeanKearney-g7d 11 วันที่ผ่านมา

    reservoir sampling is cool

  • @Krankschwester
    @Krankschwester 4 หลายเดือนก่อน +2

    Why is this, O(N) solution, "optimal", when you can use a hashmap and answer queries in O(1)? Makes no sense.

  • @shoaibakhtar9194
    @shoaibakhtar9194 3 หลายเดือนก่อน

    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.

  • @johnlabarge
    @johnlabarge 4 หลายเดือนก่อน +1

    I'm confused are the numbers sorted?

  • @piyush10793
    @piyush10793 10 หลายเดือนก่อน +1

    This would be biased since we always have higher probability to select the first index that equals target right?

    • @JAIMEAYMERICHFANS
      @JAIMEAYMERICHFANS 8 หลายเดือนก่อน

      Not really. On the 2nd pass you have a 50 /50 chance to select the 1st and the same for the 2nd.

  • @KiKi-lt4pf
    @KiKi-lt4pf ปีที่แล้ว +1

    Awesomely explained🎉, but when you suddenly moved to coding my eyes got burnt because of the light theme😂

    • @crackfaang
      @crackfaang  ปีที่แล้ว +1

      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

  • @kareni7572
    @kareni7572 2 หลายเดือนก่อน

    Found this solution and explanation more intuitive than others!

  • @manojamrutharaj9071
    @manojamrutharaj9071 2 ปีที่แล้ว

    Thank you for all these videos with easy explanations.
    Will be glad if we have all Hard Leetcode problems playsist.

  • @GoNguyen-i3k
    @GoNguyen-i3k 10 หลายเดือนก่อน

    I don't understand the question, can anybody explain that? 😂

  • @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 ปีที่แล้ว +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

  • @subee128
    @subee128 8 หลายเดือนก่อน

    Thank you very much

  • @kumarlakshya4407
    @kumarlakshya4407 ปีที่แล้ว

    great explanation

  • @omaro9627
    @omaro9627 11 หลายเดือนก่อน

    nice