在一堵无限长的墙上找门的问题,求最有效率的算法
你面对着一堵墙,左右2边都无限长
墙上有一扇门,有可能在左边也有可能在右边,你只有站在门前才能看到这扇门
求找这扇门最有效率的算法 O(n),门离你初始位置有n步
我的做法是 0 1 -1 2 -2 3 -3 。。这样一直找下去
不知道对不对,请教
付上原题,希望没有理解错误
You are facing a wall that stretches infinitely in both directions. There is a door in the wall, but you know neither how far away nor in which direction. You can see the door only when you are right next to it. Design an algorithm that enables you to reach the door by walking at most O(n) steps where n is the (unknown to you) number of steps between your initial position and the door.