from: http://blog.csdn.net/caoeryingzi/article/details/6071748
题目:
n
个数字(
0,1,
…
,n-1
)形成一个圆圈,从数字
0
开始,每次从这个圆圈中删除第
m
个数字(第一个为当前数字本身,第二个为当前数字的下一个数字)。当一个数字删除后,从被删除数字的下一个继续删除第
m
个数字。求出在这个圆圈中剩下的最后一个数字。
分析:既然题目有一个数字圆圈,很自然的想法是我们用一个数据结构来模拟这个圆圈。在常用的数据结构中,我们很容易想到用环形列表。我们可以创建一个总共有
m
个数字的环形列表,然后每次从这个列表中删除第
m
个元素。
在参考代码中,我们用
STL
中
std::list
来模拟这个环形列表。由于
list
并不是一个环形的结构,因此每次跌代器扫描到列表末尾的时候,要记得把跌代器移到列表的头部。这样就是按照一个圆圈的顺序来遍历这个列表了。
这种思路需要一个有
n
个结点的环形列表来模拟这个删除的过程,因此内存开销为
O(n)
。而且这种方法每删除一个数字需要
m
步运算,总共有
n
个数字,因此总的时间复杂度是
O(mn)
。当
m
和
n
都很大的时候,这种方法是很慢的。
接下来我们试着从数学上分析出一些规律。首先定义最初的
n
个数字(
0,1,
…
,n-1
)中最后剩下的数字是关于
n
和
m
的方程为
f(n,m)
。
在这
n
个数字中,第一个被删除的数字是
m%n-1
,为简单起见记为
k
。那么删除
k
之后的剩下
n-1
的数字为
0,1,
…
,k-1,k+1,
…
,n-1
,并且下一个开始计数的数字是
k+1
。相当于在剩下的序列中,
k+1
排到最前面,从而形成序列
k+1,
…
,n-1,0,
…
k-1
。该序列最后剩下的数字也应该是关于
n
和
m
的函数。由于这个序列的规律和前面最初的序列不一样(最初的序列是从
0
开始的连续序列),因此该函数不同于前面函数,记为
f
’
(n-1,m)
。最初序列最后剩下的数字
f(n,m)
一定是剩下序列的最后剩下数字
f
’
(n-1,m)
,所以
f(n,m)=f
’
(n-1,m)
。
接下来我们把剩下的的这
n-1
个数字的序列
k+1,
…
,n-1,0,
…
k-1
作一个映射,映射的结果是形成一个从
0
到
n-2
的序列:
k+1
->
0
k+2
->
1
…
n-1
->
n-k-2
0
->
n-k-1
…
k-1
->
n-2
把映射定义为
p
,则
p(x)= (x-k-1)%n
,即如果映射前的数字是
x
,则映射后的数字是
(x-k-1)%n
。对应的逆映射是
p-1(x)=(x+k+1)%n
。
由于映射之后的序列和最初的序列有同样的形式,都是从
0
开始的连续序列,因此仍然可以用函数
f
来表示,记为
f(n-1,m)
。根据我们的映射规则,映射之前的序列最后剩下的数字
f
’
(n-1,m)= p-1 [f(n-1,m)]=[f(n-1,m)+k+1]%n
。把
k=m%n-1
代入得到
f(n,m)=f
’
(n-1,m)=[f(n-1,m)+m]%n
。
经过上面复杂的分析,我们终于找到一个递归的公式。要得到
n
个数字的序列的最后剩下的数字,只需要得到
n-1
个数字的序列的最后剩下的数字,并可以依此类推。当
n=1
时,也就是序列中开始只有一个数字
0
,那么很显然最后剩下的数字就是
0
。我们把这种关系表示为:
0
n=1
f(n,m)={
[f(n-1,m)+m]%n
n>1
尽管得到这个公式的分析过程非常复杂,但它用递归或者循环都很容易实现。最重要的是,这是一种时间复杂度为
O(n)
,空间复杂度为
O(1)
的方法,因此无论在时间上还是空间上都优于前面的思路。
思路一的参考代码:
///////////////////////////////////////////////////////////////////////
// n integers (0, 1, ... n - 1) form a circle. Remove the mth from
// the circle at every time. Find the last number remaining
// Input: n - the number of integers in the circle initially
//
m - remove the mth number at every time
// Output: the last number remaining when the input is valid,
//
otherwise -1
///////////////////////////////////////////////////////////////////////
int LastRemaining_Solution1(
unsigned
int
n,
unsigned
int
m)
{
// invalid input
if
(n < 1 || m < 1)
return
-1;
unsigned
int
i = 0;
// initiate a list with n integers (0, 1, ... n - 1)
list<
int
> integers;
for
(i = 0; i < n; ++ i)
integers.push_back(i);
list<
int
>::iterator curinteger = integers.begin();
while
(integers.size() > 1)
{
// find the mth integer. Note that std::list is not a circle
// so we should handle it manually
for
(
int
i = 1; i < m; ++ i)
{
curinteger ++;
if
(curinteger == integers.end())
curinteger = integers.begin();
}
// remove the mth integer. Note that std::list is not a circle
// so we should handle it manually
list<
int
>::iterator nextinteger = ++ curinteger;
if
(nextinteger == integers.end())
nextinteger = integers.begin();
-- curinteger;
integers.erase(curinteger);
curinteger = nextinteger;
}
return
*(curinteger);
}
思路二的参考代码:
///////////////////////////////////////////////////////////////////////
// n integers (0, 1, ... n - 1) form a circle. Remove the mth from
// the circle at every time. Find the last number remaining
// Input: n - the number of integers in the circle initially
//
m - remove the mth number at every time
// Output: the last number remaining when the input is valid,
//
otherwise -1
///////////////////////////////////////////////////////////////////////
int LastRemaining_Solution2(
int
n,
unsigned
int
m)
{
// invalid input
if
(n <= 0 || m < 0)
return
-1;
// if there are only one integer in the circle initially,
// of course the last remaining one is 0
int
lastinteger = 0;
// find the last remaining one in the circle with n integers
for
(
int
i = 2; i <= n; i ++)
lastinteger = (lastinteger + m) % i;
return
lastinteger;
}
如果对两种思路的时间复杂度感兴趣的读者可以把
n
和
m
的值设的稍微大一点,比如十万这个数量级的数字,运行的时候就能明显感觉出这两种思路写出来的代码时间效率大不一样。