diff --git a/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/README.md b/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/README.md index 250578d05..93bc138b0 100755 --- a/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/README.md +++ b/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/README.md @@ -1,28 +1,44 @@ # [1823.Find the Winner of the Circular Game][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. -**Example 1:** +The rules of the game are as follows: -``` -Input: a = "11", b = "1" -Output: "100" -``` +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:** -## 题解 +![1](./ic234-q2-ex11.png) -### 思路1 -> ... -Find the Winner of the Circular Game -```go ``` +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. +``` ## 结语 diff --git a/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution.go b/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution.go index d115ccf5e..8f5775dfa 100644 --- a/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution.go +++ b/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution.go @@ -1,5 +1,9 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, k int) int { + winner := 0 + for i := 2; i <= n; i++ { + winner = (winner + k) % i + } + return winner + 1 } diff --git a/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution_test.go b/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution_test.go index 14ff50eb4..c4c39690a 100644 --- a/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution_test.go +++ b/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n, k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, 2, 3}, + {"TestCase2", 6, 5, 1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.n, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/ic234-q2-ex11.png b/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/ic234-q2-ex11.png new file mode 100644 index 000000000..420de3142 Binary files /dev/null and b/leetcode/1801-1900/1823.Find-the-Winner-of-the-Circular-Game/ic234-q2-ex11.png differ