Wednesday, June 13, 2012

Eggdrops

Solve the classical Google's interview problem.

Dynamic programming


k: Number of floors
n: Number of Eggs
eggDrop(n, k): Minimum number of trails needed to find the critical
               floor in worst case.


1) 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
2) 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.

eggDrop(n, k) = 1 + min{max(eggDrop(n - 1, x - 1), eggDrop(n, k - x)): 
                 x in {1, 2, ..., k}}

No comments:

Post a Comment