当前位置:网站首页>Leetcode-1823: find the winner of the game
Leetcode-1823: find the winner of the game
2022-06-24 10:23:00 【Ugly and ugly】
Title Description
share n We play games together . All the guys in a circle , Press Clockwise order from 1 To n Number . To be precise , From i If you move one person clockwise, you will arrive at the (i+1) The location of famous friends , among 1 <= i < n , From n If you move one person clockwise, you will go back to the first 1 The location of famous friends .
The game follows the following rules :
From 1 Start at the location of the first child partner .
Count clockwise k A little friend , When counting, you need to include the starting partner . Count in circles one by one , Some buddies may be counted more than once .
The last one you count needs to leave the circle , And it's like losing the game .
If there is still more than one partner in the circle , From the little friend who just lost Clockwise, the next little friend starts , Back to step 2 Carry on .
otherwise , The last one in the circle wins the game .
Give you the total number of players in the game n , And an integer k , Back to the winner of the game .
Example
Example 1:
Input :n = 5, k = 2
Output :3
explain : The operation steps of the game are as follows :
- Little friends 1 Start .
- Number of punctures in time 2 A little friend , That's my little friend 1 and 2 .
- buddy 2 Get out of the loop . Next time I'm a kid 3 Start .
- Number of punctures in time 2 A little friend , That's my little friend 3 and 4 .
- buddy 4 Get out of the loop . Next time I'm a kid 5 Start .
- Number of punctures in time 2 A little friend , That's my little friend 5 and 1 .
- buddy 1 Get out of the loop . Next time I'm a kid 3 Start .
- Number of punctures in time 2 A little friend , That's my little friend 3 and 5 .
- buddy 5 Get out of the loop . All that's left is little friends 3 . So my little friend 3 It's the winner of the game .
Example 2:
Input :n = 6, k = 5
Output :1
explain : The order in which little friends leave the circle :5、4、6、2、3 . buddy 1 It's the winner of the game .
The problem solving process
Ideas and steps
(1) The typical Joseph Ring problem , It can be considered as a closed loop queue ;
(2) First the n A little friend follows 1 --> n In the order of , The team leader element is 1, The element at the end of the team is 5;
(3) Loop through the queue , First take the team leader element out of the team , Determine whether the current counter is equal to 0, If not equal to 0, It indicates that the small partner in this position needs to participate in the next count , Then join the small partner in the team , That is, insert it at the end of the team ; If the counter is equal to 0, It means that the small partner in this position has lost , Need to leave the circle , At the same time, reset the counter to k;
(4) Each cycle requires the counter to be decremented 1, And it should be noted that the counter must be decremented first 1 operation , That is, the preceding --;
(5) Stop the loop when there is only one element left in the queue , Return the current team header element .
Code display
public class FindTheWinner {
public int findTheWinner(int n, int k) {
// Initialize queue
Queue<Integer> queue = new LinkedList<>();
for(int i = 1; i <= n; ++i) {
queue.add(i);
}
// Counter
int tmp = k;
// The remaining 1 A small partner stops the cycle
while (queue.size() > 1) {
int num = queue.poll();
if (--tmp != 0){
// Not the loser , Join the team again
queue.add(num);
} else {
// Reset counter
tmp = k;
}
}
return queue.peek();
}
public static void main(String[] args) {
FindTheWinner findTheWinner = new FindTheWinner();
System.out.println(findTheWinner.findTheWinner(5, 2));
}
}
边栏推荐
- PHP encapsulates a file upload class (supports single file and multiple file uploads)
- uniapp 开发微信公众号,下拉框默认选中列表第一个
- 引擎国产化适配&重构笔记
- 2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
- 3.员工的增删改查
- 2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
- 6. package management business development
- 一群骷髅在飞canvas动画js特效
- The difference between static link library and dynamic link library
- Regular matching mailbox
猜你喜欢

自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏

numpy.linspace()

Uniapp implements the function of clicking to make a call

JMeter接口测试工具基础— 使用Badboy录制JMeter脚本

24. 图像拼接大作业

411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency

Flink集群搭建以及企业级yarn集群搭建

uniapp 开发微信公众号,下拉框默认选中列表第一个

SQL Sever中的窗口函数row_number()rank()dense_rank()

一群骷髅在飞canvas动画js特效
随机推荐
canvas无限扫描js特效代码
引擎国产化适配&重构笔记
分布式事务原理以及解决分布式事务方案
[EI分享] 2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
leetCode-1823: 找出游戏的获胜者
4.分类管理业务开发
Learning to organize using kindeditor rich text editor in PHP
机器学习——主成分分析(PCA)
numpy. logical_ and()
Leetcode-1089: replication zero
Status of the thread pool
[input method] so far, there are so many Chinese character input methods!
leetCode-1089: 复写零
tf.contrib.layers.batch_norm
How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
Phpstrom code formatting settings
leetCode-498: 對角線遍曆
Distributed | how to make "secret calls" with dble
How large and medium-sized enterprises build their own monitoring system
学习使用php实现无限极评论和无限极转二级评论解决方案
