Solution: Egg Dropping Problem
We'll cover the following...
We'll cover the following...
Solution #1: Brute Force #
When we drop an egg from floor x, there can be two cases: (1) The egg breaks (2) The egg doesn’t break.
- If the egg breaks after dropping from xth floor, then we only need to check for floors lower than x with remaining eggs; so the problem reduces to x-1 floors and n-1 eggs
- If the egg doesn’t break after dropping from the xth floor, then we only need to check for floors higher than x; so the problem reduces to k-x floors and n eggs.
Since we need to minimize the number of trials in worst case, we take the maximum of two cases. We consider the max of the above two cases for every floor, and choose the floor that yields the minimum number of trials.
Pseudocode #
eggDrop(eggs, floors)
return 1 + min{max(eggDrop(eggs - 1, floors - 1), eggDrop(eggs, ...