当前位置:网站首页>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();
}
}
边栏推荐
- Experimental reproduction of variable image compression with a scale hyperprior
- Implementation of Weibo system based on SSM
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
- What are the skills of spot gold analysis?
- Redis环境搭建和使用的方法
- MySQL约束与多表查询实例分析
- 剑指 Offer 47. 礼物的最大价值
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- Matlab uses audioread and sound to read and play WAV files
- Construction and maintenance of business websites [12]
猜你喜欢

321. Chessboard segmentation (2D interval DP)

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

matlab 实现语音信号重采样和归一化,并播放比对效果

matlab 使用 resample 完成重采样

Feature extraction and detection 16 brisk feature detection and matching

遷移雲計算工作負載的四個基本策略

【LeetCode 43】236. The nearest common ancestor of binary tree

Learn basic K-line diagram knowledge in three minutes

MySQL constraints and multi table query example analysis

matlab 使用 audioread 、 sound 读取和播放 wav 文件
随机推荐
leetcode373. Find and minimum k-pair numbers (medium)
Medical management system (C language course for freshmen)
2022 Q2 - 提昇技能的技巧總結
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
Matlab uses resample to complete resampling
Is the knowledge of University useless and outdated?
The difference between new and malloc
321. Chessboard segmentation (2D interval DP)
JMeter (II) - install the custom thread groups plug-in
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
正则表达式学习笔记
剑指 Offer 29. 顺时针打印矩阵
matlab 使用 audioread 、 sound 读取和播放 wav 文件
Experimental reproduction of variable image compression with a scale hyperprior
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
leetcode373. 查找和最小的 K 对数字(中等)
Three core problems of concurrent programming
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
Exception handling of class C in yyds dry goods inventory
Selection of field types for creating tables in MySQL database