Skip to content

Commit d17d49e

Browse files
authored
Merge pull request #1220 from 0xff-dev/2929
Add solution and test-cases for problem 2929
2 parents d78a68e + c3f7bca commit d17d49e

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

leetcode/2901-3000/2929.Distribute-Candies-Among-Children-II/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [2929.Distribute Candies Among Children II][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+
You are given two positive integers `n` and `limit`.
5+
6+
Return the **total number** of ways to distribute `n` candies among `3` children such that no child gets more than `limit` candies.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: n = 5, limit = 2
12+
Output: 3
13+
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).
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**Example 2:**
1917

20-
### 思路1
21-
> ...
22-
Distribute Candies Among Children II
23-
```go
2418
```
25-
19+
Input: n = 3, limit = 3
20+
Output: 10
21+
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).
22+
```
2623

2724
## 结语
2825

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

3-
func Solution(x bool) bool {
4-
return x
3+
func binom(a, b int) int64 {
4+
n, k := int64(a), int64(b)
5+
if n < 0 || k < 0 || n < k {
6+
return 0
7+
}
8+
if k > n-k {
9+
k = n - k
10+
}
11+
var result int64 = 1
12+
for i := int64(1); i <= k; i++ {
13+
result = result * (n - i + 1) / i
14+
}
15+
return result
16+
}
17+
18+
func Solution(n int, limit int) int64 {
19+
total := binom(n+2, 2)
20+
21+
n1 := n - (limit + 1)
22+
n2 := n - 2*(limit+1)
23+
n3 := n - 3*(limit+1)
24+
25+
total -= 3 * binom(n1+2, 2)
26+
total += 3 * binom(n2+2, 2)
27+
total -= binom(n3+2, 2)
28+
29+
return total
530
}

leetcode/2901-3000/2929.Distribute-Candies-Among-Children-II/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n, limit int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 5, 2, 3},
17+
{"TestCase2", 3, 3, 10},
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.limit)
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.limit)
2827
}
2928
})
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)