Egg Dropping Dynamic Programming

Egg Dropping Dynamic Programming

200,568 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@ayush_shaz
@ayush_shaz - 15.04.2019 12:08

Nice solution but not easy for beginner to understand easily

Ответить
@sanjaykumarsahu5155
@sanjaykumarsahu5155 - 16.05.2019 05:49

Nice Explanation

Ответить
@ceacaralex3994
@ceacaralex3994 - 19.05.2019 04:04

actually this is first time i've seen someone posted a detailed solution for this problem instead just a stupid math equation without any explanation. good work. i m officially a fan

Ответить
@nimishgupta5289
@nimishgupta5289 - 14.08.2019 15:13

why did we calculate min and max?? Reason??

Ответить
@swagatparida7367
@swagatparida7367 - 21.08.2019 22:30

Tushar Roy is God of algorithms... He explains in a way no one else does

Ответить
@sukeshr6629
@sukeshr6629 - 03.09.2019 14:49

Why are we adding 1+ with max..

Ответить
@pksingh484
@pksingh484 - 08.09.2019 03:06

You explained the solution so well, it truly helped me understand the solution.

Ответить
@JuanGomez-uu6wf
@JuanGomez-uu6wf - 20.09.2019 19:49

MISTAKE (always droop from the middle if you have more than one egg):


There is a fundamental logical MISTAKE and while it does not affect the result, it does simplify the solution when realized. See:
If you have more than one egg, you can start drooping the first egg from any of the N floors. So, you evaluate the cost of dropping from each floor and stay with the floor that yields the minimum cost (min).
But when you think better, that is completely unnecessary: you don't need to evaluate all the floors, because the middle floor will always yield the minimum drooping cost. Always!
Now, depending on N (even or odd), the middle floor might or might not have an equal number of floors above and below. When it does not, you stay with the worst case scenery: solve the problem with more floors (max).
Applying this logic, you eliminate the min operation that evaluates all possible floors (go always with the middle) and the solution to the problem cuts down as follows:


def eggs(N,e):
if e==1:
return N
if N==1:
return 1
mid=math.ceil(N/2)
if (N-mid)>(mid-1):
return eggs(N-mid,e)+1
else:
return eggs(mid-1,e-1)+1

Explanation of the middle selection: Suppose you have 100 floors and just 2 eggs. You droop the first egg from 99: in the best case it does not break and with the remaining egg you scan the only floor left, the floor 100 (the one above you). So the best case is 2 droops! But in the worst case (it did not break), you have to scan 98 floors below you one by one with the only remaining egg. This makes 98 droops for the worst case. Thus, you are risking a lot (too much difference between the best and the worst case, and you don’t know what the case will be). So:


99: 2 (best)-98(worst)
98: 3(best)-97(worst)
97: 4(best)-96(worst)
.
.
4: 4(best)-96(worst)
3: 3(best)-97(worst)
2: 2 (best)-98(worst)


Look! When you go downwards the risk reduces (the difference between best and worst case tends to zero) but, it happens like that also when you go upwards. So, in the middle point the risk will be near zero (depending of N being even or odd). But, in any case, the middle point (as Buddha said) will always be the most neutral or best option to droop any time you have more than one egg (the one with minimum cost).

Ответить
@sanjivmadhavan5705
@sanjivmadhavan5705 - 24.09.2019 09:45

guy looks like a mad scientist

Ответить
@ShabnamKhan-cj4zc
@ShabnamKhan-cj4zc - 10.11.2019 17:37

The first video which explains the problem with a logical explanation instead of moving to equation.Thanks a lot, Tushar for making video and putting ur effort on it.

Ответить
@shawnmofid7131
@shawnmofid7131 - 03.01.2020 18:08

This was a good explanation of this problem. I really liked it and learned a lot. Thank you. Some problem statements ask for the maximum number of floors which can be covered at each step. That is the inverse of the minim attempts I guess. So in case of six floors/2eggs, the maximum floors at each step is 2. Right?

Ответить
@joe5865
@joe5865 - 05.01.2020 23:03

I just wanna know what kind of this sturdy egg is

Ответить
@rakeshsinha9541
@rakeshsinha9541 - 29.01.2020 12:45

Great explanation

Ответить
@parkershaw8529
@parkershaw8529 - 10.02.2020 02:00

The real question is, does the egg stay the same after surviving repeated drops? Hard to say....

Ответить
@yueuppp957
@yueuppp957 - 24.02.2020 02:44

Your video is great but TLE happened using your code.

Ответить
@NeverGiveUp186
@NeverGiveUp186 - 09.04.2020 20:41

Simply awesome!!

Ответить
@koreancaptain2955
@koreancaptain2955 - 13.04.2020 11:04

why we are adding 1 in max()

Ответить
@mayankbisht8638
@mayankbisht8638 - 19.04.2020 19:10

Instructions unclear, I broke all the eggs.

Ответить
@deekshaagrawal344
@deekshaagrawal344 - 30.04.2020 08:54

Well explained.

Ответить
@ranajitmukherjee9789
@ranajitmukherjee9789 - 14.05.2020 11:31

This isn't a practical solution

Ответить
@hsinghal179
@hsinghal179 - 22.05.2020 13:18

Sir, we can even find out using binary search, so if n is number of eggs, and f is number of floors, time complexity will be O(nlog(f)) and space complexity will be O(1).

Ответить
@balajiarumugam1876
@balajiarumugam1876 - 03.06.2020 19:04

Bro I have a doubt.. that Whether all the eggs to be dropped from the same floor?

Ответить
@anirudhatalmale5575
@anirudhatalmale5575 - 07.06.2020 10:16

Super awesome

Ответить
@souvikbiswas2787
@souvikbiswas2787 - 12.07.2020 09:22

you dont need to be a foreigner being indian, try to spell and pronounce properly rather than talking in foreign style....

Ответить
@darshanbari2439
@darshanbari2439 - 18.07.2020 17:50

Fuck carryminati, mi and my bois watch Tushar Roy - Coding Made Simple.

Ответить
@Official-tk3nc
@Official-tk3nc - 25.07.2020 10:36

I am from NIT jalandhar and You>????

Ответить
@alonivshin3980
@alonivshin3980 - 26.07.2020 20:04

Tushar #1

Ответить
@nitishraj-pj2ef
@nitishraj-pj2ef - 29.07.2020 17:13

Any improved version cause I'm getting TLE...

Ответить
@prateeksaxena198
@prateeksaxena198 - 15.08.2020 00:40

I am getting a TLE with this logic :(

Ответить
@HimalayanRover-nb4mh
@HimalayanRover-nb4mh - 15.08.2020 22:00

East or west Tushar is the best!! Thank you

Ответить
@mihirkumarsrivastava1895
@mihirkumarsrivastava1895 - 23.08.2020 21:59

you are a great teacher you have helped me a lot, i am very much thankful to you.

Ответить
@nikhillingam4630
@nikhillingam4630 - 30.08.2020 10:36

Nice Explanation for egg breaking

Ответить
@prashantjoshi160
@prashantjoshi160 - 19.09.2020 11:40

Tushar,

There is a flaw in your video in the sense that the explanation you gave is for dynamic programming using a nxk matrix while your formula is based on recursion.
In the explanation you had used the following formula -

f[n][k] = min(1 + max( f[n-1][j-1] , f[n][j-x] ) where x ranges from 1 to j )

Here n = number of eggs and k = number of floors

Ответить
@marcelasena5108
@marcelasena5108 - 17.10.2020 20:56

Thanks for taking the time to break it down and explain it step by step!

Ответить
@RTX_valorant
@RTX_valorant - 28.10.2020 10:48

Guy he is wrong;
I tried it today it broke on first floor itself so only one egg is needed😐

Ответить
@madhurjoshi4113
@madhurjoshi4113 - 15.03.2021 15:10

this man is too good.

Ответить
@kushwanthkapa2041
@kushwanthkapa2041 - 05.04.2021 04:21

time complexity is O(n^3)

Ответить
@mrigendra6261
@mrigendra6261 - 23.06.2021 07:29

Can you please tell me how you decide if egg breaks or not. everyone is talking about egg breaks but the question is what condition we should put while coding to know if the egg breaks or not?

Ответить
@dhruv6084
@dhruv6084 - 08.09.2021 07:37

Nice explanation and very much greatful to you, best video of this question

Ответить
@dhanashreegodase4445
@dhanashreegodase4445 - 10.12.2021 10:11

Thank you very much!

Ответить
@myvinbarboza3038
@myvinbarboza3038 - 26.05.2022 16:21

Tushar Roy 5years later still the best solution to the problem. YOU ROCK!!!

Ответить
@ritik5604
@ritik5604 - 17.06.2022 11:12

How to solve it with O(n*k) TC?

Ответить
@coldstone87
@coldstone87 - 19.06.2022 17:00

I didnt under why did he use max in equation. If someone understood please explain.

Ответить
@Coldstone88
@Coldstone88 - 21.07.2022 15:07

Awesome.

Ответить
@RaviKumar-vk6ib
@RaviKumar-vk6ib - 08.07.2023 18:26

great work but your solution doesn't pass all test cases in leetcode just fyi

Ответить
@userx0r
@userx0r - 26.07.2024 13:58

Copilot take me to this vedio 🤔

Ответить