当前位置:网站首页>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
边栏推荐
- Combined with case: the usage of the lowest API (processfunction) in Flink framework
- Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
- flink sql-client. SH tutorial
- 一文概览2D人体姿态估计
- One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
- 各大主流编程语言性能PK,结果出乎意料
- LVGL 8.2 Line
- Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
- Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
- Docker compose public network deployment redis sentinel mode
猜你喜欢
程序员自曝接私活:10个月时间接了30多个单子,纯收入40万
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
[information retrieval] experiment of classification and clustering
曝光一下阿里的工资待遇和职位级别
No servers available for service: xxxx
LVGL 8.2 LED
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
An overview of 2D human posture estimation
随机推荐
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
Programmer turns direction
Ali was laid off employees, looking for a job n day, headhunters came bad news
为什么国产手机用户换下一部手机时,都选择了iPhone?
尊重他人的行为
LVLG 8.2 circular scrolling animation of a label
如何搭建一支搞垮公司的技术团队?
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
深度学习7 Transformer系列实例分割Mask2Former
LVGL 8.2 Sorting a List using up and down buttons
现代控制理论入门+理解
Summary of common problems in development
No servers available for service: xxxx
潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
Data Lake (13): spark and iceberg integrate DDL operations
LVGL 8.2 keyboard
SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!
C language personal address book management system