当前位置:网站首页>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
边栏推荐
- C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
- AI 从代码中自动生成注释文档
- Image watermarking, scaling and conversion of an input stream
- AcWing 345. 牛站 题解(floyd的性质、倍增)
- JS ES5也可以创建常量?
- golang 基础 —— 数据类型
- AcWing 1140. Shortest network (minimum spanning tree)
- AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
- AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
- 拖拽改变顺序
猜你喜欢

今日问题-2022/7/4 lambda体中修改String引用类型变量

Appium automation test foundation uiautomatorviewer positioning tool

Right mouse button customization

Let's see how to realize BP neural network in Matlab toolbox

Today's question -2022/7/4 modify string reference type variables in lambda body

蓝桥杯2022年第十三届省赛真题-积木画

我如何编码8个小时而不会感到疲倦。

Mongodb checks whether the table is imported successfully

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

永久的摇篮
随机推荐
IDEA常用的快捷键
grep查找进程时,忽略grep进程本身
POJ 3177 redundant paths POJ 3352 road construction (dual connection)
uva 1401 dp+Trie
Add PDF Title floating window
【信号与系统】
拖拽改变顺序
New job insights ~ leave the old and welcome the new~
图片打水印 缩放 和一个输入流的转换
golang 基础 —— 数据类型
Make Jar, Not War
糊涂工具类(hutool)post请求设置body参数为json数据
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
C语言【23道】经典面试题【下】
交叉验证如何防止过拟合
黑马笔记---异常处理
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
shell脚本快速统计项目代码行数
WCF基金会