|
| 1 | +# Egg Drop Dynamic Problem |
| 2 | + |
| 3 | +## Problem Description |
| 4 | + |
| 5 | +Given some number of floors and some number of eggs, what is the minimum number of attempts it will take to find out from which floor egg will break. |
| 6 | + |
| 7 | +Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: |
| 8 | + |
| 9 | +- An egg that survives a fall can be used again. |
| 10 | +- A broken egg must be discarded. |
| 11 | +- The effect of a fall is the same for all eggs. |
| 12 | +- If an egg breaks when dropped, then it would break if dropped from a higher floor. |
| 13 | +- If an egg survives a fall then it would survive a shorter fall. |
| 14 | + |
| 15 | +If only one egg is available and we wish to be sure of obtaining the right result, the experiment can be carried out in only one way. Drop the egg from the first-floor window; if it survives, drop it from the second floor window. Continue upward until it breaks. In the worst case, this method may require 36 droppings. Suppose 2 eggs are available. What is the least number of egg-droppings that is guaranteed to work in all cases? |
| 16 | +The problem is not actually to find the critical floor, but merely to decide floors from which eggs should be dropped so that total number of trials are minimized. |
| 17 | + |
| 18 | +## Solution |
| 19 | +- eggNumber -> Number of eggs at the moment |
| 20 | +- floorNumber -> Floor number at the moment |
| 21 | +- visitingFloor -> Floor being visited at the moment |
| 22 | +- attempts -> Minimum number of attempts it will take to find out from which floor egg will break |
| 23 | + |
| 24 | +We store all the solutions in a 2D array. Where rows represents number of eggs and columns represent number of floors. |
| 25 | + |
| 26 | +First, we set base cases: |
| 27 | +1) If there's only one egg, it takes as many attempts as number of floors |
| 28 | +2) If there are two eggs and one floor, it takes one attempt |
| 29 | + |
| 30 | +```swift |
| 31 | +for var floorNumber in (0..<(numberOfFloors+1)){ |
| 32 | +eggFloor[1][floorNumber] = floorNumber //base case 1: if there's only one egg, it takes 'numberOfFloors' attempts |
| 33 | +} |
| 34 | + |
| 35 | +eggFloor[2][1] = 1 //base case 2: if there are two eggs and one floor, it takes one attempt |
| 36 | +``` |
| 37 | + |
| 38 | +When we drop an egg from a floor 'floorNumber', there can be two cases (1) The egg breaks (2) The egg doesn’t break. |
| 39 | + |
| 40 | +1) If the egg breaks after dropping from 'visitingFloorth' floor, then we only need to check for floors lower than 'visitingFloor' with remaining eggs; so the problem reduces to 'visitingFloor'-1 floors and 'eggNumber'-1 eggs. |
| 41 | +2) If the egg doesn’t break after dropping from the 'visitingFloorth' floor, then we only need to check for floors higher than 'visitingFloor'; so the problem reduces to floors-'visitingFloor' floors and 'eggNumber' eggs. |
| 42 | + |
| 43 | +Since we need to minimize the number of trials in worst case, we take the maximum of two cases. We consider the max of above two cases for every floor and choose the floor which yields minimum number of trials. |
| 44 | + |
| 45 | +We find the answer based on the base cases and previously found answers as follows. |
| 46 | +```swift |
| 47 | +attempts = 1 + max(eggFloor[eggNumber-1][floors-1], eggFloor[eggNumber][floorNumber-floors])//we add one taking into account the attempt we're taking at the moment |
| 48 | + |
| 49 | +if attempts < eggFloor[eggNumber][floorNumber]{ //finding the min |
| 50 | + eggFloor[eggNumber][floorNumber] = attempts; |
| 51 | +} |
| 52 | +``` |
| 53 | +## Example |
| 54 | +Let's assume we have 2 eggs and 2 floors. |
| 55 | +1) We drop one egg from the first floor. If it breaks, then we get the answer. If it doesn't we'll have 2 eggs and 1 floors to work with. |
| 56 | + attempts = 1 + maximum of 0(got the answer) and eggFloor[2][1] (base case 2 which gives us 1) |
| 57 | + attempts = 1 + 1 = 2 |
| 58 | +2) We drop one egg from the second floor. If it breaks, we'll have 1 egg and 1 floors to work with. If it doesn't, we'll get the answer. |
| 59 | + attempts = 1 + maximum of eggFloor[1][1](base case 1 which gives us 1) and 0(got the answer) |
| 60 | + attempts = 1 + 1 = 2 |
| 61 | +3) Finding the minimum of 2 and 2 gives us 2, so the answer is 2. |
| 62 | + 2 is the minimum number of attempts it will take to find out from which floor egg will break. |
| 63 | + |
| 64 | + |
0 commit comments