Skip to content

Commit 2ab530e

Browse files
committed
Add solution and test-cases for problem 2226
1 parent f0a9eb1 commit 2ab530e

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [2226.Maximum Candies Allocated to K Children][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 a **0-indexed** integer array `candies`. Each element in the array denotes a pile of candies of size `candies[i]`. You can divide each pile into any number of **sub piles**, but you **cannot** merge two piles together.
5+
6+
You are also given an integer `k`. You should allocate piles of candies to `k` children such that each child gets the **same** number of candies. Each child can be allocated candies from **only one** pile of candies and some piles of candies may go unused.
7+
8+
Return the **maximum number of candies** each child can get.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: candies = [5,8,6], k = 3
14+
Output: 5
15+
Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Candies Allocated to K Children
23-
```go
2420
```
25-
21+
Input: candies = [2,5], k = 11
22+
Output: 0
23+
Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.
24+
```
2625

2726
## 结语
2827

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"slices"
5+
"sort"
6+
)
7+
8+
func Solution(candies []int, k int64) int {
9+
m := slices.Max(candies)
10+
var ok func(int) bool
11+
ok = func(n int) bool {
12+
cnt := int64(0)
13+
for _, c := range candies {
14+
cnt += int64(c / n)
15+
}
16+
return cnt >= k
17+
}
18+
// 0, 1, 2, 3, ,4
19+
// 1, 2, 3, 4, ,5
20+
x := sort.Search(m+1, func(i int) bool {
21+
return !ok(i + 1)
22+
})
423
return x
524
}

leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
candies []int
14+
k int64
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{5, 8, 6}, 3, 5},
18+
{"TestCase2", []int{2, 5}, 11, 0},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.candies, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.candies, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)