|
1 | 1 | # [1823.Find the Winner of the Circular Game][title]
|
2 | 2 |
|
3 |
| -> [!WARNING|style:flat] |
4 |
| -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) |
5 |
| -
|
6 | 3 | ## Description
|
| 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<sup>th</sup> friend brings you to the `(i+1)`<sup>th</sup> friend for `1 <= i < n`, and moving clockwise from the n<sup>th</sup>. friend brings you to the 1<sup>st</sup> friend. |
7 | 5 |
|
8 |
| -**Example 1:** |
| 6 | +The rules of the game are as follows: |
9 | 7 |
|
10 |
| -``` |
11 |
| -Input: a = "11", b = "1" |
12 |
| -Output: "100" |
13 |
| -``` |
| 8 | +1. **Start** at the 1<sup>st</sup> friend. |
| 9 | +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. |
| 10 | +3. The last friend you counted leaves the circle and loses the game. |
| 11 | +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. |
| 12 | +5. Else, the last friend in the circle wins the game. |
| 13 | + |
| 14 | +Given the number of friends, `n`, and an integer `k`, return the winner of the game. |
14 | 15 |
|
15 |
| -## 题意 |
16 |
| -> ... |
| 16 | +**Example 1:** |
17 | 17 |
|
18 |
| -## 题解 |
| 18 | + |
19 | 19 |
|
20 |
| -### 思路1 |
21 |
| -> ... |
22 |
| -Find the Winner of the Circular Game |
23 |
| -```go |
24 | 20 | ```
|
| 21 | +Input: n = 5, k = 2 |
| 22 | +Output: 3 |
| 23 | +Explanation: Here are the steps of the game: |
| 24 | +1) Start at friend 1. |
| 25 | +2) Count 2 friends clockwise, which are friends 1 and 2. |
| 26 | +3) Friend 2 leaves the circle. Next start is friend 3. |
| 27 | +4) Count 2 friends clockwise, which are friends 3 and 4. |
| 28 | +5) Friend 4 leaves the circle. Next start is friend 5. |
| 29 | +6) Count 2 friends clockwise, which are friends 5 and 1. |
| 30 | +7) Friend 1 leaves the circle. Next start is friend 3. |
| 31 | +8) Count 2 friends clockwise, which are friends 3 and 5. |
| 32 | +9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner. |
| 33 | +``` |
| 34 | + |
| 35 | +**Example 2:** |
25 | 36 |
|
| 37 | +``` |
| 38 | +Input: n = 6, k = 5 |
| 39 | +Output: 1 |
| 40 | +Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1. |
| 41 | +``` |
26 | 42 |
|
27 | 43 | ## 结语
|
28 | 44 |
|
|
0 commit comments