Generalised Two-Egg Puzzle

前端 未结 5 1046
面向向阳花
面向向阳花 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:16

    From Wiki Egg Dropping puzzle we know that the the state transfer equation is:

    W(n,k) = 1 + min{ max(W(n − 1, x − 1), W(n,k − x)) } , x = 1, 2, ..., k

    W(n,1)=1, W(1,k)=k

    n = number of test eggs available

    k = number of (consecutive) floors yet to be tested

    Below is my understanding.

    We have k floors, n eggs, assume we use an egg to test in x floor. there are only two possible results:

    1. it breaks, so the problem recursively come to: x-1 floors, n-1 eggs, which reflects to W(n-1,x-1)
    2. it doesn't break, so the problem recursively come to: k-x floors, n eggs, which reflects to W(n,k-x)

    Since the problem requires the worst case, we have to choose the bigger one to ensure the worst case works, that's why we add an max between W(n-1,x-1) and W(n,k-x).

    Besides, as we just assumed testing in x floor, x can be from 1 to k, in this situation, we definitely need to choose the minimum to ensure the min experimental drops to find out N, that's why we add an min between {max(W(n − 1, x − 1), W(n,k − x)): x = 1, 2, ..., k}

    Finally, as we have used 1 drop in x floor, so the equation must add 1, which reflects to the first part of the equation.

    Hope that solves your puzzle :-)

提交回复
热议问题