当前位置:网站首页>LeetCode. Sword finger offer 62 The last remaining number in the circle
LeetCode. Sword finger offer 62 The last remaining number in the circle
2022-07-07 01:45:00 【Stingy Wolf】
List of articles
Title Description
0,1,···,n-1 this n Number in a circle , From numbers 0 Start , Delete the... From this circle every time m A digital ( Delete and count from the next number ). Find the last number left in the circle .
for example ,0、1、2、3、4 this 5 Numbers make a circle , From numbers 0 Start deleting the 3 A digital , Before deleting 4 The numbers in turn are 2、0、4、1, So the last remaining number is 3.
Ideas
This is a famous mathematical problem —— Joseph ring .
The solution is directly posted here ( There is no strict mathematical proof , But it is easier to understand and remember )
Go back in time , Start with the last situation , Push back .
Borrow the picture of a big man :
- The last round ( It is called the n round ), There is only one number left ( be called
x
) when , The total size of the array is 1,x
The subscript of is 0 - n-1 In the round ,
x
Is safe , We draw the whole ring into an infinite straight line , It's easy to know , The starting point of this round , To x, Yes m Number , namely x There is m Number , And what is removed in this round is x The previous number . In this roundx
The subscript of is(m + 0) % 2
( stay x Fill the front with m Number ), Because of the ring , Let's take a model . This round is reserved 2 Number . - n-2 In the round , The first number of the last round was retained , explain n-1 There is m Number , And what is removed in this round is the number in front of this number . Then add m Number , be n-1 The subscript of all numbers in the wheel should be moved to the right m, So in this round
x
The subscript of is ,(((m + 0) % 2) + m) % 3
- …
- The first 1 round , Revert to the original state , We can work out
x
Subscript in initial round
class Solution {
public int lastRemaining(int n, int m) {
// The subscript of the last round
int x = 0;
for (int i = 2; i <= n; i++) {
x = (x + m) % i;
}
return x;
}
}
Formula for : 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], The boundary condition is f ( 1 , m ) = 0 f(1,m)=0 f(1,m)=0
Rigorous mathematical derivation , Reference resources : This is probably the most detailed mathematical derivation of Joseph ring you can find ! - You know
边栏推荐
- Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
- 长按按钮执行函数
- Vocabulary in Data Book
- When grep looks for a process, it ignores the grep process itself
- JS ES5也可以创建常量?
- Ds-5/rvds4.0 variable initialization error
- [signal and system]
- 制作带照明的DIY焊接排烟器
- 剑指 Offer II 035. 最小时间差-快速排序加数据转换
- Compile command line terminal swift
猜你喜欢
JVM 内存模型
修改px4飞控的系统时间
454 Baidu Mianjing 1
域分析工具BloodHound的使用说明
Yunna | work order management measures, how to carry out work order management
[advanced C language] 8 written questions of pointer
Analyze "C language" [advanced] paid knowledge [End]
设置Wordpress伪静态连接(无宝塔)
454-百度面经1
Comparison of picture beds of free white whoring
随机推荐
JVM 内存模型
C语言实例_5
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
Public key \ private SSH avoid password login
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
Google发布安全更新,修复Chrome中已被利用的0 day
AcWing 361. 观光奶牛 题解(spfa求正环)
736. LISP syntax parsing: DFS simulation questions
一起看看matlab工具箱内部是如何实现BP神经网络的
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
Reptile practice (VI): novel of climbing pen interesting Pavilion
刨析《C语言》【进阶】付费知识【一】
黑马笔记---异常处理
使用nodejs完成判断哪些项目打包+发版
Taro applet enables wxml code compression
1123. 最深叶节点的最近公共祖先
dvajs的基础介绍及使用
DS-5/RVDS4.0变量初始化错误
Match VIM from zero (0) -- Introduction to vimscript
Hutool post requests to set the body parameter to JSON data