Two egg dropping puzzle variation: unknown/infinite floors

后端 未结 2 1034
忘了有多久
忘了有多久 2021-01-27 10:50

Preface

This problem was inspired by a similar question last week on SO, that got deleted before it was clear what the real question was. I think this

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-27 11:40

    There is a simple method that has O(sqrt(f*)) complexity. Make your nth step to be n floors up, that is, check floors 1, 3 (1 + 2), 6 (1 + 2 + 3), etc. This way at the nth step you will be on n*(n+1)/2 floor, and you will reach f* in n = O(sqrt(f*)) steps.

    Then for the second egg you will need to go n single steps over your last step in stage 1, which will add another O(sqrt(f*)).

    If O(sqrt(k)) was optimal for known k, this method has to be optimal in terms of complexity, too.

提交回复
热议问题