1823. Find the Winner of the Circular Game Medium

@problem@discussion
#Array#Math#Recursion#Queue#Simulation



1/**
2 * [1823] Find the Winner of the Circular Game
3 *
4 * 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 i^th friend brings you to the (i+1)^th friend for 1 <= i < n, and moving clockwise from the n^th friend brings you to the 1^st friend.
5 * The rules of the game are as follows:
6 * <ol>
7 * 	Start at the 1^st friend.
8 * 	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.
9 * 	The last friend you counted leaves the circle and loses the game.
10 * 	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.
11 * 	Else, the last friend in the circle wins the game.
12 * </ol>
13 * Given the number of friends, n, and an integer k, return the winner of the game.
14 *  
15 * Example 1:
16 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
17 * Input: n = 5, k = 2
18 * Output: 3
19 * Explanation: Here are the steps of the game:
20 * 1) Start at friend 1.
21 * 2) Count 2 friends clockwise, which are friends 1 and 2.
22 * 3) Friend 2 leaves the circle. Next start is friend 3.
23 * 4) Count 2 friends clockwise, which are friends 3 and 4.
24 * 5) Friend 4 leaves the circle. Next start is friend 5.
25 * 6) Count 2 friends clockwise, which are friends 5 and 1.
26 * 7) Friend 1 leaves the circle. Next start is friend 3.
27 * 8) Count 2 friends clockwise, which are friends 3 and 5.
28 * 9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
29 * Example 2:
30 * 
31 * Input: n = 6, k = 5
32 * Output: 1
33 * Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= k <= n <= 500
39 * 
40 *  
41 * Follow up:
42 * Could you solve this problem in linear time with constant space?
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/find-the-winner-of-the-circular-game/
48// discuss: https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53    pub fn find_the_winner(n: i32, k: i32) -> i32 {
54        0
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_1823() {
66    }
67}
68


Back
© 2025 bowen.ge All Rights Reserved.