Leetcode 1823.找出游戏的获胜者

news/2024/10/23 7:34:27/

原题链接:Leetcode 1823. There are n friends that are playing a game.

The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend.

The rules of the game are as follows:

  1. Start at the 1st friend.
  2. Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.
  3. The last friend you counted leaves the circle and loses the game.
  4. If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat.
  5. Else, the last friend in the circle wins the game.
    Given the number of friends, n, and an integer k, return the winner of the game.

Example 1:
在这里插入图片描述

Input: n = 5, k = 2
Output: 3
Explanation: Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.

Example 2:

Input: n = 6, k = 5
Output: 1
Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.

Constraints:

  • 1 <= k <= n <= 500


方法一:队列+模拟

思路:

模拟游戏的过程:
首先将玩家加入到一个队列当中,对于给定的K,执行K-1次将队头移动到队尾的操作,此时就是要淘汰的人。将他出队。最后队列中剩下的唯一元素就是胜者,输出即可。

c++代码:

class Solution {
public:int findTheWinner(int n, int k) {queue<int> Q;for(int i = 1; i <= n; i++ ){Q.push(i);}while(Q.size() > 1){// k-1轮 将元素从队头放到队尾for(int i = 1; i < k; i++ ){Q.push(Q.front());Q.pop();}// 队头出队 表示淘汰Q.pop();}return Q.front();}
};

复杂度分析:

  • 时间复杂度:O(nk),需要淘汰n-1个人,淘汰每个人需要循环k-1次
  • 空间复杂度:O(n),用一个队列来存储元素

http://www.ppmy.cn/news/703476.html

相关文章

poj1823 - hotel

http://poj.org/problem?id1823 Hotel Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 2389 Accepted: 1044 Description The “Informatics” hotel is one of the most luxurious hotels from Galaciuc. A lot of tourists arrive or leave this hotel i…

hdu-1823 Luck and Love

题目链接:hdu1823二维线段树单点更新区间查询 题意 向一个100*1000的二维空间中插入点,每次查询时,查询区间最大值. 题解 身高既然是100~200,那就相当于100;活泼度相当于1000.所以建立100*1000的二维线段树. 大坑有如下几个输出-1,而不是-1.0输入的h1,h2,a1,a2,大小不一定,需要…

hdu1823

/* 分析&#xff1a; 二维线段树。 很早就明白什么是二维线段树了&#xff0c;不过没有见过二维线段树的代码&#xff0c; 想趁这次敲一个&#xff0c;不过最后发现敲的有那么一点儿点儿毛病&#xff0c;感觉没有 像二维树状数组那样发挥出二维情况下的线段树的速度&#xff0c…

poj 1823

题意&#xff1a;一个hotel&#xff0c;有n间连续的房间&#xff0c;现在有m组操作&#xff1a; type 1: 1, a, b: 第a个房间起的b个房间有旅客入住。 type 2: 2, a, b: 第a个房间起的b个房间的旅客离开。 type 3: 3: 问最长的连续空房间有多少间。 思路&#xff1a;…

codeforces 1354A 菜鸟题解

codeforces 1354A 菜鸟题解 题目链接&#xff1a;https://codeforc.es/problemset/problem/1354/A 题目解释&#xff1a; 输入为a b c d 总共要睡a分钟&#xff1b;每次休息时间为b分钟&#xff1b;闹铃每次设置后c分钟响&#xff1b;每次被闹铃吵醒需要d分钟睡着&#xff1…

Quectel EC200A-CN移植

Quectel EC200A-CN移植 一:usb转串口二:usb网卡驱动三:源码修改四:测试 一:usb转串口 usb-serial-option,USB转串口驱动&#xff0c;生产/dev/ttyUSB0-2,分别是DM&#xff0c;AT&#xff0c;PPP 需要使能内核选项如下: CONFIG_USB_SERIALy CONFIG_USB_SERIAL_WWANy CONFIG_US…

倍福TwinCAT Error starting TwinCAT System ADS 1823 / 0x71f

If ADS error 1823 (0x71f) occurs when an IP stack TcCOM object is started, the configuration of the network card is probably incorrect. Check the settings under “Adapter” for the network card in the project folder:

1823. 数组的最长前缀

1823. 数组的最长前缀 给定两个正整数X和Y&#xff0c;以及正整数数组nums。 我们需要找到一个最大的index&#xff0c;使得在nums[0], nums[1], .... , nums[index]中&#xff0c;出现X、Y的次数相等&#xff0c;且至少均出现一次&#xff0c;返回该index。 若不存在这样的ind…