面试题62
Last updated
Was this helpful?
Last updated
Was this helpful?
0,1,,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字。求出这个圆圈里剩下的最后一个数字。
例如,0、1、2、3、4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字依次是2、0、4、1,因此最后剩下的数字是3。
1 <= n <= 10^5
1 <= m <= 10^6
Josephus problem
reference:
suppose the index(after the dead one) of the winner in the current round is x
, since the dead one in the previous round is removed, all indexes after are moved forward(with modulo) m
steps, thus the index of the winner in the previous round is (x + m) % (size of previous round)
.