当前位置:网站首页>LeetCode. 剑指offer 62. 圆圈中最后剩下的数
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
2022-07-06 17:52:00 【抠脚的大灰狼】
题目描述
0,1,···,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字(删除后从下一个数字开始计数)。求出这个圆圈里剩下的最后一个数字。
例如,0、1、2、3、4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字依次是2、0、4、1,因此最后剩下的数字是3。
思路
这是一个著名的数学问题 —— 约瑟夫环。
这里直接贴出解法(没有严格的数学证明,但比较容易理解和记忆)
用时光倒流法,从最后的情况开始,往回推。
借用一位大佬的图:

- 最后一轮(称之为第n轮),只剩一个数字(称之为
x)时,数组总大小为1,x的下标为0 - n-1轮中,
x是安全的,我们把整个环拉成一条无限长的直线,则容易知道,这一轮的起点,到x,有m个数,即x前面有m个数,且这一轮中移除的是x前面的那个数。那么该轮中x的下标为(m + 0) % 2(在x前面补上m个数),由于有环,我们取个模。这一轮保留了2个数。 - n-2轮中,上一轮的第一个数被保留了下来,说明n-1轮的第一个数前面有m个数,且该轮移除的是这个数前面的那个数。则在整个数组前面补上m个数,则n-1轮中所有数的下标都要往右移m,所以该轮中
x的下标为,(((m + 0) % 2) + m) % 3 - …
- 第1轮,还原到初始状态,就能求出
x在初始轮中的下标
class Solution {
public int lastRemaining(int n, int m) {
// 最后一轮的下标
int x = 0;
for (int i = 2; i <= n; i++) {
x = (x + m) % i;
}
return x;
}
}
公式为: f ( n , m ) = [ f ( n − 1 , m ) + m ] f(n,m) = [f(n-1,m) + m] % n f(n,m)=[f(n−1,m)+m],边界条件为 f ( 1 , m ) = 0 f(1,m)=0 f(1,m)=0
严谨的数学推导,参考:这或许是你能找到的最详细约瑟夫环数学推导! - 知乎
边栏推荐
- Gnet: notes on the use of a lightweight and high-performance go network framework
- [JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
- MySQL script batch queries all tables containing specified field types in the database
- Can the system hibernation file be deleted? How to delete the system hibernation file
- C# 计算农历日期方法 2022
- docker 方法安装mysql
- Installation of torch and torch vision in pytorch
- LLDP兼容CDP功能配置
- Yunna | work order management measures, how to carry out work order management
- Let's see through the network i/o model from beginning to end
猜你喜欢

微信公众号发送模板消息

Js逆向——捅了【马蜂窝】的ob混淆与加速乐
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

Transformation transformation operator

Do you understand this patch of the interface control devaxpress WinForms skin editor?

LLDP兼容CDP功能配置
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)

HMM notes
![[Niuke] b-complete square](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[Niuke] b-complete square

Typical problems of subnet division and super network construction
随机推荐
AI automatically generates annotation documents from code
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
如何管理分布式团队?
LeetCode:1175. 质数排列
Neon Optimization: performance optimization FAQ QA
table表格设置圆角
Dark horse notes - exception handling
Installation and testing of pyflink
一起看看matlab工具箱内部是如何实现BP神经网络的
c语言—数组
域分析工具BloodHound的使用说明
Case development of landlord fighting game
Using the entry level of DVA in taro3.*
[case sharing] basic function configuration of network loop detection
今日问题-2022/7/4 lambda体中修改String引用类型变量
Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
Lldp compatible CDP function configuration
Dynamic planning idea "from getting started to giving up"
JTAG principle of arm bare board debugging