剑指offer62 圆圈中最后剩下的数字【约瑟夫问题】

题目描述

0,1,···,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字(删除后从下一个数字开始计数)。求出这个圆圈里剩下的最后一个数字。

例如,0、1、2、3、4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字依次是2、0、4、1,因此最后剩下的数字是3。

示例 1:

1
2
输入: n = 5, m = 3
输出: 3

示例 2:

1
2
输入: n = 10, m = 17
输出: 2

限制:

  • 1 <= n <= 10^5
  • 1 <= m <= 10^6

解题思路

[约瑟夫问题]

方法一:数学+递归

递归方程

$$
f(n,m)=f(n-1,m)+m) % n
$$

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
public int lastRemaining(int n, int m) {
return f(n, m);
}

private int f(int n, int m) {
if (n == 1) {
return 0;
}
int x = f(n - 1, m);
System.out.println(x);
return (m + x) % n;
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(n) #递归栈

方法二:数学+迭代

使用的迭代的方法优化递归。

代码实现

1
2
3
4
5
6
7
public int lastRemaining2(int n, int m) {
int f = 0;
for (int i = 2; i <= n; ++i) {
f = (m + f) % i;
}
return f;
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(1)

问题

本题是找出最后剩下的那个数,如何找到第n个淘汰的数呢?如何得出每一轮淘汰的数呢?

资料

剑指offer62 圆圈中最后剩下的数字【约瑟夫问题】

http://example.com/2021/06/06/剑指offer62-圆圈中最后剩下的数字/

Author

John Doe

Posted on

2021-06-06

Updated on

2021-06-08

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.