Solution: Egg Dropping Problem
The lesson describes the various approaches to solve the egg dropping problem.
Solution #1: Brute force
Let’s start by looking at the brute force solution to this problem:
Explanation
When we drop an egg from a floor x, there can be two cases:
-
The egg breaks.
-
The egg doesn’t break.
-
If the egg breaks after dropping it from
xth floor, we only need to check for floors lower thanxwith the remaining eggs; so, the problem reduces tox-1floors andn-1eggs (line 24). -
If the egg doesn’t break after dropping it from the
xth floor, we only need to check for floors higher thanx; so, the problem reduces tok-xfloors andneggs (line 24).
Since we need to minimize the number of trials in the worst case, we take the maximum from both cases (line 24). We consider the max of the above two cases for every floor and choose the one which yields the minimum number of trials (lines 25-26).
Solution #2: Memoization
The solution above, even though it is correct, results in redundant recursive calls which can be avoided if the solution is memoized:
Explanation
In the memoized approach, we use a 2-D array, lookupTable ...