当前位置:网站首页>Sword finger offer 62 The last remaining number in the circle
Sword finger offer 62 The last remaining number in the circle
2022-07-02 02:00:00 【Yake1965】
The finger of the sword Offer 62. The last number in the circle


lastRemaining(n - 1, m) yes n - 1 Number , namely 0, 1, …, n - 2, From numbers 0 Start , Delete the... From this circle every time m A digital ( Delete and count from the next number ). And the real sequence m, m + 1, …, n - 2, 0, …, m - 2 Make an innuendo .x -> (m + x) % n.
Method 1 : recursive
class Solution:
# Python The default recursion depth is not enough , Manual setting required
sys.setrecursionlimit(100000)
def lastRemaining(self, n: int, m: int) -> int:
if n == 0: return 0
x = self.lastRemaining(n - 1, m)
return (m + x) % n
class Solution {
public int lastRemaining(int n, int m) {
if(n == 1) return 0;
int index = lastRemaining(n - 1, m); // x Is in accordance with the 0, 1, 2 ... n - 2 The result of the calculation
return (index + m) % n; // Make an innuendo with the actual
}
}
Method 2 : iteration
law : Delete the... In each round m The number is the first m - 1 The number is moved to the end of the array for circulation , Every time i It's equivalent to moving left m A place .
trace to its source : From the last time index by 0 Start tracing back , Let in turn index Situated i Move right m A place , That is to add m.
Every time in reverse order index + m You can find the one in the last round i Where index, At the same time, it is necessary to take the remainder of the round array to prevent cross-border size
class Solution {
public int lastRemaining(int n, int m) {
int index = 0; // Extrapolate from the last remaining number , Finally get 0, 1, ... , n - 1 Index in .
for (int i = 2; i <= n; ++i) {
index = (m + index) % i;
}
return index;
}
}
Method 3 : Simulation , use ArrayList simulation : Time complexity O(n^2)
class Solution {
public int lastRemaining(int n, int m) {
List<Integer> list = new ArrayList<>(); // LinkedList Overtime
for (int i = 0; i < n; i++) {
list.add(i);}
int index = 0;
while (n > 1) {
index = (index + m - 1) % n;
list.remove(index);
n--;
}
return list.get(0);
}
}
1823. Find the winner of the game
Method 1 : recursive
class Solution {
public int findTheWinner(int n, int k) {
if(n == 1) return 1;
int x = findTheWinner(n - 1, k); // x Convert to index
return (k + x - 1) % n + 1;
}
}
Method 2 : iteration
class Solution {
public int findTheWinner(int n, int k) {
int winner = 1;
for (int i = 2; i <= n; i++) {
winner = (k + winner - 1) % i + 1;
}
return winner;
}
}
Method 3 : Queue simulation
class Solution {
public int findTheWinner(int n, int k) {
Deque<Integer> q = new ArrayDeque<>();
for(int i = 1; i <= n; i++) q.add(i);
while(q.size() > 1){
for(int i = 0; i < k - 1; i++){
q.offer(q.pop());
}
q.pop();
}
return q.peek();
}
}
边栏推荐
- Ubuntu20.04 PostgreSQL 14 installation configuration record
- [Maya] the error of importing Maya into Metahuman
- What is the MySQL column to row function
- What is AQS and its principle
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- 开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
- 2022 Q2 - Summary of skills to improve skills
- Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
- leetcode2312. 卖木头块(困难,周赛)
- The concept, function, characteristics, creation and deletion of MySQL constraints
猜你喜欢

企业应该选择无服务器计算吗?

AR增强现实可应用的场景

【LeetCode 43】236. The nearest common ancestor of binary tree
![[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology

Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months

并发编程的三大核心问题

leetcode2310. 个位数字为 K 的整数之和(中等,周赛)

RTL8189FS如何关闭Debug信息

Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?

matlab 使用 audioread 、 sound 读取和播放 wav 文件
随机推荐
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
Construction and maintenance of business websites [15]
The concept, function, characteristics, creation and deletion of MySQL constraints
Selection of field types for creating tables in MySQL database
479. Additive binary tree (interval DP on the tree)
Medical management system (C language course for freshmen)
np.where 和 torch.where 用法
Should enterprises choose server free computing?
Learn basic K-line diagram knowledge in three minutes
SQLite 3 of embedded database
Golang lock
321. Chessboard segmentation (2D interval DP)
1222. Password dropping (interval DP, bracket matching)
大学的知识是否学而无用、过时?
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
正则表达式学习笔记
Based on configured schedule, the given trigger will never fire
[question] - why is optical flow not good for static scenes
【深度学习】infomap 人脸聚类 facecluster
* and & symbols in C language