Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I agree with 14 as the optimal answer, now I'm pondering what could be done if you had THREE lightbulbs


It's a fairly cool recursive problem. Let N be the number of floors.

OneBulb(N) = N. With one bulb and 100 floors, you have no choice but to drop the bulb 100 times, once for each floor.

With two bulbs, a single drop at floor K has two outcomes. If the bulb survives, then you can consider all the floors above K as a separate building, and start again with two bulbs and N - K floors. If the bulb breaks, you can consider all the floors below K as a separate building, and start again with ONE bulb and K - 1 floors. So:

    TwoBulbs(N) = min { 1 <= K <= N } ( max(TwoBulbs(N-K), OneBulb(K-1)) ) + 1
Now, the same is true when you go to three bulbs:

    ThreeBulbs(N) = min { 1 <= K <= N } ( max(ThreeBulbs(N-K), TwoBulbs(K-1)) ) + 1
And the recursion continues.

I wrote a small Perl script which solves for arbitrary B (number of bulbs) and N (number of floors). For B = 3, N = 100, you can do it in a worst case scenario of 9 drops.

    3 bulbs. 100 floors.
    drops[3][100] = 9 drops.
            Drop bulb at floor 8.
            Bulb survives: consider floors 9 to 100 as a separate 92-floor building and observe drops[3][92] = 8 drops.
            Bulb breaks: consider floors 1 to 7 as a separate 7-floor building and observe drops[2][7] = 4 drops.


It can be done in 9 tries with 3 light bulbs




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: