Skip to content

Commit 433ae70

Browse files
authored
Merge pull request #891 from 0xff-dev/1823
Add solution and test-cases for problem 1823
2 parents fbfd5ce + 8954745 commit 433ae70

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
# [1823.Find the Winner of the Circular Game][title]
22

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-
63
## 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.
75

8-
**Example 1:**
6+
The rules of the game are as follows:
97

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.
1415

15-
## 题意
16-
> ...
16+
**Example 1:**
1717

18-
## 题解
18+
![1](./ic234-q2-ex11.png)
1919

20-
### 思路1
21-
> ...
22-
Find the Winner of the Circular Game
23-
```go
2420
```
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:**
2536

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+
```
2642

2743
## 结语
2844

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, k int) int {
4+
winner := 0
5+
for i := 2; i <= n; i++ {
6+
winner = (winner + k) % i
7+
}
8+
return winner + 1
59
}

leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n, k int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 5, 2, 3},
17+
{"TestCase2", 6, 5, 1},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.n, c.k)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.n, c.k)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}
Loading

0 commit comments

Comments
 (0)