Skip to content

Add solution and test-cases for problem 2928 #1221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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).
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading