Guessing a number knowing only if the number proposed is lower or higher?

前端 未结 7 982
悲哀的现实
悲哀的现实 2021-01-02 21:34

I need to guess a number. I can only see if the number I\'m proposing is lower or higher. Performance matters a whole lot, so I thought of the following algorithm:

Le

7条回答
  •  失恋的感觉
    2021-01-02 22:13

    A standard binary search between 0 and N(N is the given number) will give you the answer in logN time.

提交回复
热议问题