当前位置:网站首页>约瑟夫环问题
约瑟夫环问题
2022-07-29 06:05:00 【小卢要刷力扣题】
约瑟夫环问题
约瑟夫环问题
给定一个链表头节点head,和一个正数m
从头开始,每次数到m就杀死当前节点
然后被杀节点的下一个节点从1开始重新数,
周而复始直到只剩一个节点,返回最后的节点
思路
先解决一个问题:
编号和报数之间的关系


有abcd四个人,第一轮的编号是
a:1, b:2 , c:3 , d:4
当淘汰b之后
第二轮的编号就变成
a:3, d:2, c:1
剩一个点的时候存活节点在最后一步的编号中它是编号1
假设有一个公式,传入一个节点淘汰之后的编号,返回淘汰之前的编号
一直调用到一个节点也不淘汰的时候的编号就是幸存者编号
目标就是:推导一个F函数 告诉我淘汰之后的编号怎么对应出淘汰之前的编号
画图像
首先求出一个数字对应的编号
剃刀感觉的图像

根据函数图像的规则:左加右减,上加下减
推出 编号=(数-1)%i+1
给你一个数字报数它减1之后模上i再加1.就得到了它的编号
淘汰之后的编号怎么推淘汰之前的编号
假设i个节点淘汰报数到m的编号
用具体例子


抽象化


延长
限制死定义域

向左移动s位
s是长度i 链表中数到m淘汰人的编号

将刚才算出的编号函数套进去
最后化简到
前=(后+m-1)%i+1
力扣原题:剑指 Offer 62. 圆圈中最后剩下的数字
0,1,···,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字(删除后从下一个数字开始计数)。求出这个圆圈里剩下的最后一个数字。
例如,0、1、2、3、4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字依次是2、0、4、1,因此最后剩下的数字是3
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public int lastRemaining(int n, int m) {
return getLive(n,m)-1;
}
public int getLive(int n,int m){
if(n==1){
return 1;
}
return (getLive(n-1,m)+m-1)%n+1;
}
}
//迭代
public int lastRemaining(int n, int m) {
int ans = 1;
int r = 1;
while (r <= n) {
ans = (ans + m - 1) % (r++) + 1;
}
return ans - 1;
}
边栏推荐
- 如何优雅的写 Controller 层代码?
- Teacher wangshuyao's notes on operations research 06 linear programming and simplex method (geometric significance)
- Teacher Wu Enda machine learning course notes 01 introduction
- How to write controller layer code gracefully?
- pytest合集(7)— 参数化
- IDEA找不到Database解决方法
- 【冷冻电镜入门】加州理工公开课课程笔记 Part 3: Image Formation
- Relative date used by filter in salesforce
- 要不要满足客户所有的需求
- Basic knowledge of MySQL (high frequency interview questions)
猜你喜欢

Some tips of vim text editor

建木持续集成平台v2.5.2发布

Unity free element special effect recommendation

C language memory stack and heap usage

Thread - thread safety - thread optimization

Unity免费元素特效推荐

DM data guard cluster setup

vim文本编辑器的一些使用小技巧

Software definition boundary SDP

Ali gave several SQL messages and asked how many tree search operations need to be performed?
随机推荐
【解决方案】ERROR: lib/bridge_generated.dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
Thread synchronization - producers and consumers, tortoise and rabbit race, dual thread printing
[CF1054H] Epic Convolution——数论,卷积,任意模数NTT
我的创业邻居们
Simulation volume leetcode [normal] 081. Search rotation sort array II
Teacher Wu Enda's machine learning course notes 02 univariate linear regression
MySQL:当你CRUD时BufferPool中发生了什么?十张图就能说清楚
Leetcode-592: fraction addition and subtraction
buck电路boot和ph引脚实测
Implementation of DDP cluster distributed training under pytoch multi GPU conditions (brief introduction - from scratch)
Unity探索地块通路设计分析 & 流程+代码具体实现
Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
Decompilation of wechat applet
CVPR2022Oral专题系列(一):低光增强
线程 - 线程安全 - 线程优化
Cesium反射
线程同步—— 生产者与消费者、龟兔赛跑、双线程打印
Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)
Talk about tcp/ip protocol? And the role of each layer?