当前位置:网站首页>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,xThe subscript of is 0 - n-1 In the round ,
xIs 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 roundxThe 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
xThe subscript of is ,(((m + 0) % 2) + m) % 3 - …
- The first 1 round , Revert to the original state , We can work out
xSubscript 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
边栏推荐
- 我如何编码8个小时而不会感到疲倦。
- 机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
- Match VIM from zero (0) -- Introduction to vimscript
- How to prevent overfitting in cross validation
- Typical problems of subnet division and super network construction
- 黑马笔记---异常处理
- 移植DAC芯片MCP4725驱动到NUC980
- swiper组件中使用video导致全屏错位
- Using the entry level of DVA in taro3.*
- Set up [redis in centos7.x]
猜你喜欢

刨析《C语言》【进阶】付费知识【一】

454-百度面经1

新工作感悟~辞旧迎新~

The difference between Tansig and logsig. Why does BP like to use Tansig

Appium自动化测试基础 — uiautomatorviewer定位工具

According to the analysis of the Internet industry in 2022, how to choose a suitable position?

Comparison of picture beds of free white whoring

js如何快速创建一个长度为 n 的数组

Appium基础 — Appium Inspector定位工具(一)

Box stretch and pull (left-right mode)
随机推荐
Let's see how to realize BP neural network in Matlab toolbox
今日问题-2022/7/4 lambda体中修改String引用类型变量
Match VIM from zero (0) -- Introduction to vimscript
mongodb查看表是否导入成功
C语言实例_4
子网划分、构造超网 典型题
AcWing 904. 虫洞 题解(spfa求负环)
刨析《C语言》【进阶】付费知识【一】
First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
DS-5/RVDS4.0变量初始化错误
The cradle of eternity
Ds-5/rvds4.0 variable initialization error
Compile command line terminal swift
Taro applet enables wxml code compression
mysqlbackup 还原特定的表
黑马笔记---创建不可变集合与Stream流
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
AcWing 345. 牛站 题解(floyd的性质、倍增)
Gin introduction practice
一文带你走进【内存泄漏】