Skip to content

Commit 6664453

Browse files
committed
solutions: 1823 - Find the Winner of the Circular Game (Medium)
1 parent 228458f commit 6664453

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
description: "Author: @wingkwong | https://leetcode.com/problems/find-the-winner-of-the-circular-game/"
3+
tags: [Array, Math, Recursion, Queue, Simulation]
4+
---
5+
6+
# 1823 - Find the Winner of the Circular Game (Medium)
7+
8+
## Problem Link
9+
10+
https://leetcode.com/problems/find-the-winner-of-the-circular-game/
11+
12+
## Problem Statement
13+
14+
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.
15+
16+
The rules of the game are as follows:
17+
18+
1. **Start** at the `1st` friend.
19+
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.
20+
3. The last friend you counted leaves the circle and loses the game.
21+
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.
22+
5. Else, the last friend in the circle wins the game.
23+
24+
Given the number of friends, `n`, and an integer `k`, return _the winner of the game_.
25+
26+
**Example 1:**
27+
28+
```
29+
Input: n = 5, k = 2
30+
Output: 3
31+
Explanation: Here are the steps of the game:
32+
1) Start at friend 1.
33+
2) Count 2 friends clockwise, which are friends 1 and 2.
34+
3) Friend 2 leaves the circle. Next start is friend 3.
35+
4) Count 2 friends clockwise, which are friends 3 and 4.
36+
5) Friend 4 leaves the circle. Next start is friend 5.
37+
6) Count 2 friends clockwise, which are friends 5 and 1.
38+
7) Friend 1 leaves the circle. Next start is friend 3.
39+
8) Count 2 friends clockwise, which are friends 3 and 5.
40+
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
41+
```
42+
43+
**Example 2:**
44+
45+
```
46+
Input: n = 6, k = 5
47+
Output: 1
48+
Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
49+
```
50+
51+
**Constraints:**
52+
53+
- `1 <= k <= n <= 500`
54+
55+
**Follow up:**
56+
57+
Could you solve this problem in linear time with constant space?
58+
59+
## Approach 1: Recursion
60+
61+
Since the constraints are small, we can just simulate the process by using recursion. Let's say $go(n, k)$ represents the index of the winner where there are $n$ and a step size of $k$. At the beginning, there are $n$ people, each round we know that one of them will be eliminated, so the state goes to $go(n - 1, k)$ and $k$ remains unchanged.
62+
63+
We know that the next $k$ friends in the clockwise direction will leave so we add $k$ to the current state. Since it could be exceed $n$, we can simply take the mod of $n$. At the end, we add $1$ because of 1-index base.
64+
65+
<Tabs>
66+
<TabItem value="py" label="Python">
67+
<SolutionAuthor name="@wingkwong"/>
68+
69+
```py
70+
class Solution:
71+
def findTheWinner(self, n: int, k: int) -> int:
72+
def go(n, k):
73+
if n == 1: return 0
74+
return (go(n - 1, k) + k) % n
75+
return go(n, k) + 1
76+
```
77+
78+
</TabItem>
79+
</Tabs>

0 commit comments

Comments
 (0)