Generalised Two-Egg Puzzle

前端 未结 5 1048
面向向阳花
面向向阳花 2020-12-28 11:02

Here is the Problem Description :

Suppose that we wish to know which stories in a N-story building are safe to drop eggs from, and which will cause the eggs to break

5条回答
  •  情深已故
    2020-12-28 11:07

    This problem can be solved with following 3 approaches (that I am know) :

    1. Dynamic Programming
    2. Solution using Binary Search Tree
    3. Solution by obtaining the direct mathematical formula for maximum number of floors that can be tested or covered with given number of eggs and given number of drops

    Let me first define some symbols that are used in analysis done afterwards :

    e = number of eggs
    f = number of floors in building
    n = number of egg drops 
    Fmax(e, n) = maximum number of floors that can be tested or covered with e eggs and n drops
    

    The crux for dynamic programming approach lies in following recursive formula for Fmax:

    Fmax(e, n) = 1 + Fmax(e-1, n-1) + fmax(e, n-1)
    

    And the crux for obtaining the direct mathematical formula for Fmax lies in following recursive formula for Fmax:

    Fmax(e, n) = { ∑Fmax(e-1,i) for i = 1 to n } - Fmax(e-1, n) + n 
    

    Alternative solution using Binary Search Tree (BST) is also possible for this problem. In order to facilitate our analysis, let us draw BST with slight modifications as follows:

    1.    If egg breaks then child node is drawn on left down side
    2.    If egg does not break then child node is drawn straight down side
    

    If we draw BST with above kind of representation then width of the BST represents the number of eggs.

    Any BST with f number of nodes, drawn with above kind of representation and subjected to the constraint width of BST <= e (number of eggs) is a solution but it may not be the optimal solution.

    Hence obtaining the optimal solution is equivalent to obtaining the arrangement of nodes in BST with minimum height subjected to the constraint: width of BST <= e

    For more details about all the above 3 approaches, check my blog at: 3 approaches for solving generalized egg drop problem

提交回复
热议问题