880. Floor Testing Drops

A lab tests how sturdy its protective capsules are using a tower with n floors numbered 1 to n. There is an unknown critical floor t with 0 <= t <= n: a capsule dropped from any floor above t breaks, and one dropped from floor t or lower survives unharmed and can be reused. A broken capsule cannot be used again. You own k identical capsules.

Return the minimum number of drops that guarantees you can determine t exactly, in the worst case, using an optimal strategy.

Example 1

Input:
k = 2, n = 6
Output:
3
Explanation:

With 2 capsules, m drops can cover m + m(m-1)/2 floors; m = 3 covers 6, m = 2 covers only 3, so 3 drops are needed.

Example 2

Input:
k = 1, n = 5
Output:
5
Explanation:

With a single capsule you must climb floor by floor: 5 drops in the worst case.

Example 3

Input:
k = 3, n = 14
Output:
4
Explanation:

With 3 capsules, m drops cover C(m,1)+C(m,2)+C(m,3) floors: m = 4 covers 4+6+4 = 14, m = 3 covers only 7, so 4 drops are needed.

Constraints

  • 1 ≤ k ≤ 100
  • 1 ≤ n ≤ 107

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms

Expected complexity

Time
O(k * log n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…