当前位置:网站首页>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();
}
}
边栏推荐
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
- MySQL constraints and multi table query example analysis
- golang---锁
- Six lessons to be learned for the successful implementation of edge coding
- Medical management system (C language course for freshmen)
- Matlab uses resample to complete resampling
- Three core problems of concurrent programming
- leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
- leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
- matlab 实现语音信号重采样和归一化,并播放比对效果
猜你喜欢
leetcode2312. 卖木头块(困难,周赛)
Redis环境搭建和使用的方法
PR second training
[question] - why is optical flow not good for static scenes
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
人工智能在网络安全中的作用
Golang lock
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
Volume compression, decompression
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
随机推荐
Volume compression, decompression
Exception handling of class C in yyds dry goods inventory
如何远程、在线调试app?
How to build and use redis environment
Open那啥的搭建文档
正则表达式学习笔记
MySQL中一条SQL是怎么执行的
How to debug apps remotely and online?
Golang lock
花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
【C#】使用正则校验内容
The role of artificial intelligence in network security
How to use a product to promote "brand thrill"?
What is the MySQL column to row function
企业应该选择无服务器计算吗?
【毕业季】研究生学长分享怎样让本科更有意义
Data analysis on the disaster of Titanic
How to solve MySQL master-slave delay problem
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
MySQL主从延迟问题怎么解决