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
@Alex Petrash suggested this solution. The same idea but instead of while we use log: return math.ceil(math.log(buckets, minutesToTest / minutesToDie + 1))
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
3 жыл бұрын
hahah well the end product looks effortless but I was just as blanked out as you lol
@TrueVasember
5 ай бұрын
its not actually a coding problem man
This is the third video solution and I finally understood.
Best explaination i could find on the whole of world wide web.
Happy Diwali !!
@tripathi5174
3 жыл бұрын
same to you !
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
3 жыл бұрын
No problem and thanks a lot !! :)
OMG you are a genius! thanks for your explanation!
brilliant!!! love the explanation
One of the best explanation Thanks Mate! 🔥🔥
Amazing Explanation
thank you for the great explanation!!
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..
brooooooo hats off 🔥 🔥
Great explanation.
Thanks a lot for this
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
3 жыл бұрын
Thank you and your right using log is the same as implementing the while loop but a lot more elegant.
Great explanation
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.
youre the best man, i just want you to know that
@saianishmalla2646
3 жыл бұрын
Thanks a lot Jason :)
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?
thanks bro!
@saianishmalla2646
3 жыл бұрын
Happy to help!!
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
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
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
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.
Damnnnnn
Congratulation me ! My very first hard problem 😂
Poor pigs 🐷! Great job man...your explanation is awesome
@saianishmalla2646
3 жыл бұрын
HAHA poor pigs indeed :( and thanks a lot !!
Happy Diwali bro
@saianishmalla2646
3 жыл бұрын
Thank you and same to you!
1st test ->0+15min=15 2nd test->15+(15min)=30 3rd test->30+(15min)=45 4th test->45+(15min)=60
@saianishmalla2646
3 жыл бұрын
right on!
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
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
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.
Great Explanation Brother But please stop using this fake accent.
> ceil( log10(buckets) / log10(minutesToTest / minutesToDie + 1))