From bf0cc94778f12d00e31992281e8ca96a8e994848 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 1 Jun 2025 17:03:26 +0800 Subject: [PATCH] Add solution and test-cases for problem 2928 --- .../README.md | 25 +++++++--------- .../Solution.go | 29 +++++++++++++++++-- .../Solution_test.go | 21 +++++++------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/README.md b/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/README.md index db6244dd4..02e403bd5 100755 --- a/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/README.md +++ b/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/README.md @@ -1,28 +1,25 @@ # [2928.Distribute Candies Among Children I][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 +You are given two positive integers `n` and `limit`. + +Return the **total number** of ways to distribute `n` candies among `3` children such that no child gets more than `limit` candies. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 5, limit = 2 +Output: 3 +Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1). ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Distribute Candies Among Children I -```go ``` - +Input: n = 3, limit = 3 +Output: 10 +Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0). +``` ## 结语 diff --git a/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/Solution.go b/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/Solution.go index d115ccf5e..19c5a0c5c 100644 --- a/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/Solution.go +++ b/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +func binom(a, b int) int { + n, k := a, b + if n < 0 || k < 0 || n < k { + return 0 + } + if k > n-k { + k = n - k + } + var result int = 1 + for i := 1; i <= k; i++ { + result = result * (n - i + 1) / i + } + return result +} + +func Solution(n int, limit int) int { + total := binom(n+2, 2) + + n1 := n - (limit + 1) + n2 := n - 2*(limit+1) + n3 := n - 3*(limit+1) + + total -= 3 * binom(n1+2, 2) + total += 3 * binom(n2+2, 2) + total -= binom(n3+2, 2) + + return total } diff --git a/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/Solution_test.go b/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/Solution_test.go index 14ff50eb4..325a287d6 100644 --- a/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/Solution_test.go +++ b/leetcode/2901-3000/2928.Distribute-Candies-Among-Children-I/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + n, limit int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, 2, 3}, + {"TestCase2", 3, 3, 10}, } // 开始测试 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.limit) 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.limit) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }