Poor Pigs | Leet code 458 | Theory explained + Python code

This video is a solution to Leet code 458, Poor Pigs. I explain the question, go over how the logic / theory behind solving the question and finally solve it using Python code.
Comment below if you have a better solution to this problem!
Let me know if you have any feedback and don't forget to subscribe for more videos!
Code:
leetcode.com/problems/poor-pi...)
Time stamps:
0:00 Question
2:01 Solution Explained
18:35 Python Code
More leetcode questions solved:
• Add and Search Word - ...

Пікірлер: 45

  • @saianishmalla2646
    @saianishmalla26463 жыл бұрын

    @Alex Petrash suggested this solution. The same idea but instead of while we use log: return math.ceil(math.log(buckets, minutesToTest / minutesToDie + 1))

  • @godlike33
    @godlike333 жыл бұрын

    Bro how could you do this so effortlessly 🔥 I just got blanked after reading the question and you just finished it in 4-5 lines of code man ! Hats off 🔥🔥

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    hahah well the end product looks effortless but I was just as blanked out as you lol

  • @TrueVasember

    @TrueVasember

    5 ай бұрын

    its not actually a coding problem man

  • @Sameer.Trivedi
    @Sameer.Trivedi10 ай бұрын

    This is the third video solution and I finally understood.

  • @tusharsingh1791
    @tusharsingh1791 Жыл бұрын

    Best explaination i could find on the whole of world wide web.

  • @saianishmalla2646
    @saianishmalla26463 жыл бұрын

    Happy Diwali !!

  • @tripathi5174

    @tripathi5174

    3 жыл бұрын

    same to you !

  • @vivekcsdiabolicfreak
    @vivekcsdiabolicfreak3 жыл бұрын

    Dude, thanks for taking the time to make such a well explained video. Everyone just jumps into the code with very little explanation. Please continue making videos with an emphasis on the explanation and u will have a ton of subscribers #subscribed

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    No problem and thanks a lot !! :)

  • @charlesgreen4182
    @charlesgreen41822 жыл бұрын

    OMG you are a genius! thanks for your explanation!

  • @shrimpo6416
    @shrimpo64162 жыл бұрын

    brilliant!!! love the explanation

  • @hareeshr3979
    @hareeshr3979 Жыл бұрын

    One of the best explanation Thanks Mate! 🔥🔥

  • @nipunshah1373
    @nipunshah1373 Жыл бұрын

    Amazing Explanation

  • @cindysu262
    @cindysu262 Жыл бұрын

    thank you for the great explanation!!

  • @vgsuresh
    @vgsuresh Жыл бұрын

    one of the best explanation ... so far for the problem .. well done... just curious how the intuition for matrix and then eventually cube usage came..

  • @UNbeatanGod
    @UNbeatanGod Жыл бұрын

    brooooooo hats off 🔥 🔥

  • @nikitasinghvi5798
    @nikitasinghvi57983 жыл бұрын

    Great explanation.

  • @adityashah8966
    @adityashah8966 Жыл бұрын

    Thanks a lot for this

  • @alexpetrash8414
    @alexpetrash84143 жыл бұрын

    Thanks you explanation is really cool and clear, only one small thing that you can improve a realization, to calculate required amount of pigs you can use `log` function instead of the loop. So just make math.ceil(math.log(buckets, time_to_test / time_to_die + 1))

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    Thank you and your right using log is the same as implementing the while loop but a lot more elegant.

  • @kashif3173
    @kashif3173 Жыл бұрын

    Great explanation

  • @abhinavranjan3599
    @abhinavranjan35992 жыл бұрын

    12:51 what do you mean happening simultaneously? If 2 pigs used even simultaneously, it will take 8 tests (2 * 4) well beyond the time limit. please explain.

  • @JC_23
    @JC_233 жыл бұрын

    youre the best man, i just want you to know that

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    Thanks a lot Jason :)

  • @estifanosbireda1892
    @estifanosbireda1892 Жыл бұрын

    Wow, A really good solution with crystal clear explanation. but how do u come up the intuition? how wld u advice to approach these kinda questions?

  • @ramfattah275
    @ramfattah2753 жыл бұрын

    thanks bro!

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    Happy to help!!

  • @AbrahamWilson
    @AbrahamWilson3 жыл бұрын

    That's a really good explanation thanks. I need a bit of help, when you have 1k buckets, you'll need 5 pigs, so how do we use these 5 pigs in a 3d structures, I'm a bit confused.

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    Thanks a lot and to answer your question I used the 2d and 3d part cause I think it helps in visualising. Now let's say we have 5 pigs in that case we would actually have 5 dimensions which is hard for us to imagine instead maybe think of it as a 5x5x5x5x5 array / list maybe that helps.

  • @AbrahamWilson

    @AbrahamWilson

    3 жыл бұрын

    @@saianishmalla2646 yeah I had a feeling it would be something like that, like you said it's hard to imagine it, thanks for the reply.

  • @crankyinmv

    @crankyinmv

    3 жыл бұрын

    Mathematically: tests = floor(test time/die time) You can test (tests+1)^pigs buckets tests+1 = log base pigs (buckets) This page: www.mathsisfun.com/algebra/logarithms.html will give you the final step.

  • @dhananjoydey1337
    @dhananjoydey1337 Жыл бұрын

    Damnnnnn

  • @md.shazidalhasan6726
    @md.shazidalhasan6726 Жыл бұрын

    Congratulation me ! My very first hard problem 😂

  • @hacksfind3529
    @hacksfind35293 жыл бұрын

    Poor pigs 🐷! Great job man...your explanation is awesome

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    HAHA poor pigs indeed :( and thanks a lot !!

  • @midhileshmomidi2434
    @midhileshmomidi24343 жыл бұрын

    Happy Diwali bro

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    Thank you and same to you!

  • @anyonecancode8462
    @anyonecancode84623 жыл бұрын

    1st test ->0+15min=15 2nd test->15+(15min)=30 3rd test->30+(15min)=45 4th test->45+(15min)=60

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    right on!

  • @yelnil
    @yelnil3 жыл бұрын

    8:45 Your reasoning why you have 5 columns on each row is that you can check 4 buckets instead of 5? I dont follow.

  • @crankyinmv

    @crankyinmv

    3 жыл бұрын

    5 is the # of tests + 1. In each test, a pig can drink a column of water. The possible outcomes are either the pig dies on a particular column, in which case that column contains the poisoned bucket, or the pig lives because the poisoned bucket is in the column the pig did not drink.

  • @saianishmalla2646

    @saianishmalla2646

    3 жыл бұрын

    So, to be specific I was referring to that specific row. Going back to where we had only 5 buckets. Let's say the pig drank form bucket 1, 2, 3 and 4. And if it lives in all of those cases then we can deduce that the 5th or the untested bucket contains the poison.

  • @HarshYadav-tu4wd
    @HarshYadav-tu4wd2 жыл бұрын

    Great Explanation Brother But please stop using this fake accent.

  • @nipunshah1373
    @nipunshah1373 Жыл бұрын

    > ceil( log10(buckets) / log10(minutesToTest / minutesToDie + 1))