887 Super Egg Drop

1. Question

You are givenKeggs, and you have access to a building withNfloors from1toN.

Each egg is identical in function, and if an egg breaks, you cannot drop it again.

You know that there exists a floorFwith0 <= F <= Nsuch that any egg dropped at a floor higher thanFwill break, and any egg dropped at or below floorFwill not break.

Eachmove, you may take an egg (if you have an unbroken one) and drop it from any floorX(with 1 <= X <= N).

Your goal is to know with certainty what the value ofFis.

What is the minimum number of moves that you need to know with certainty whatFis, regardless of the initial value ofF?

Example 1:

Input: K = 1, N = 2
Output: 2
Explanation: 
Drop the egg from floor 1.  If it breaks, we know with certainty that F = 0.
Otherwise, drop the egg from floor 2.  If it breaks, we know with certainty that F = 1.
If it didn't break, then we know with certainty F = 2.
Hence, we needed 2 moves in the worst case to know what F is with certainty.

Example 2:

Example 3:

Note:

  1. 1 <= K <= 100

  2. 1 <= N <= 10000

2. Implementation

(1) Memoization

(2) Memoization + Binary Search

思路:

(3) DP

3. Time & Space Complexity

(1) Memoization: 时间复杂度O(K * N^2), 空间复杂度O(KN)

(2) Memoization + Binary Search: 时间复杂度O(K * N* logN), 空间复杂度O(KN)

(3) DP: 时间复杂度O(KlogN), 空间复杂度O(KN)

Last updated

Was this helpful?