当前位置:网站首页>Leecode learning notes - Joseph problem
Leecode learning notes - Joseph problem
2022-07-04 14:46:00 【Engineering students who like history】

class Solution {
public int lastRemaining(int n, int m) {
int ans = 0;
// The rest of the last round 2 personal , So from 2 Start backstepping
for (int i = 2; i <= n; i++) {
ans = (ans + m) % i;
}
return ans;
}
}
- explain
Help you understand
problem 1: Suppose we already know 11 Personal time , The subscript position of the winner is 6. Then the next round 10 Personal time , What is the subscript position of the winner ?
answer : In fact! , The first round of deletion is numbered 3 After , After that, everyone moved forward 3 position , Victory is also moving forward 3 position , So his subscript position is determined by 6 become 3.problem 2: Suppose we already know 10 Personal time , The subscript position of the winner is 3. Then the next round 11 Personal time , What is the subscript position of the winner ? answer :
This can be mistaken as the reverse process of the previous problem , Everyone moves back 3 position , therefore f ( 11 , 3 ) = f ( 10 , 3 ) + 3
f(11,3)=f(10,3)+3f(11,3)=f(10,3)+3. However, it is possible that the array will cross the boundary , So the number of current people on the final module ,f ( 11 , 3
) = ( f ( 10 , 3 ) + 3 ) % 11
f(11,3)=(f(10,3)+3)%11f(11,3)=(f(10,3)+3)%11
There is only one element left , Suppose this last surviving element is num, The final subscript of this element must be 0 ( Because there is only one element left ),
So if we can launch this in the last round num The subscript , Then according to the previous round num The subscript of infers the previous round num The subscript ,
Until it is inferred that the number of elements is n The round of num The subscript , Then we can get the final element according to this subscript . The inference process is as follows :
First, in the last round num The subscript of must be 0, This is known .
There should be two elements in the last round , In this round num The subscript of is (0 + m)%n = (0+3)%2 = 1; Explain before this round of deletion num The subscript of is 1;
Another round should have 3 Elements , In this round num The subscript of is (1+3)%3 = 1; It indicates that before an element is deleted in this round num The subscript of is 1;
Another round should have 4 Elements , In this round num The subscript of is (1+3)%4 = 0; It indicates that before an element is deleted in this round num The subscript of is 0;
Another round should have 5 Elements , In this round num The subscript of is (0+3)%5 = 3; It indicates that before an element is deleted in this round num The subscript of is 3;
…Because the sequence we want to delete is 0-n-1, So getting the subscript is actually getting the final result . For example, when n by 5 When ,num The initial subscript of is 3,
therefore num Namely 3, That is to say, from 0-n-1 In the sequence of , after n-1 Elimination of round ,3 This element finally survived , It is also the final result .Summarize the derivation formula :( After this round num Subscript + m) % Number of elements in the last round = Last round num The subscript
边栏推荐
- The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
- Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
- Chapter 17 process memory
- leetcode:6110. 网格图中递增路径的数目【dfs + cache】
- C language set operation
- EventBridge 在 SaaS 企业集成领域的探索与实践
- SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!
- 内存管理总结
- Some problems and ideas of data embedding point
- LVGL 8.2 List
猜你喜欢

Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model

如何搭建一支搞垮公司的技术团队?

Sqlserver functions, creation and use of stored procedures

Scratch Castle Adventure Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022

Ultrasonic distance meter based on 51 single chip microcomputer

Count the running time of PHP program and set the maximum running time of PHP
![leetcode:6109. Number of people who know the secret [definition of DP]](/img/95/03e2606b249f26db052cf5075041c1.png)
leetcode:6109. Number of people who know the secret [definition of DP]

Leetcode 61: rotating linked list

92. (cesium chapter) cesium building layering

LVGL 8.2 LED
随机推荐
信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
[information retrieval] link analysis
Red envelope activity design in e-commerce system
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
es6模块化
Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
LVGL 8.2 LED
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
MySQL stored procedure exercise
A keepalived high availability accident made me learn it again
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
Opencv learning notes - linear filtering: box filtering, mean filtering, Gaussian filtering
Classify boost libraries by function
flink sql-client. SH tutorial
EventBridge 在 SaaS 企业集成领域的探索与实践
C language set operation
leecode学习笔记-约瑟夫问题
Leetcode 61: rotating linked list
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
WT588F02B-8S(C006_03)单芯片语音ic方案为智能门铃设计降本增效赋能