From 2ab530e1604d8d98b2d234a4b666daca68211043 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 14 Mar 2025 09:16:51 +0800 Subject: [PATCH] Add solution and test-cases for problem 2226 --- .../README.md | 27 +++++++++---------- .../Solution.go | 21 ++++++++++++++- .../Solution_test.go | 22 +++++++-------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/README.md b/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/README.md index ccb82f6c1..00c002d2f 100755 --- a/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/README.md +++ b/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/README.md @@ -1,28 +1,27 @@ # [2226.Maximum Candies Allocated to K Children][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 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. + +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. + +Return the **maximum number of candies** each child can get. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: candies = [5,8,6], k = 3 +Output: 5 +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. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Maximum Candies Allocated to K Children -```go ``` - +Input: candies = [2,5], k = 11 +Output: 0 +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. +``` ## 结语 diff --git a/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution.go b/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution.go index d115ccf5e..7b307c645 100644 --- a/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution.go +++ b/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { +import ( + "slices" + "sort" +) + +func Solution(candies []int, k int64) int { + m := slices.Max(candies) + var ok func(int) bool + ok = func(n int) bool { + cnt := int64(0) + for _, c := range candies { + cnt += int64(c / n) + } + return cnt >= k + } + // 0, 1, 2, 3, ,4 + // 1, 2, 3, 4, ,5 + x := sort.Search(m+1, func(i int) bool { + return !ok(i + 1) + }) return x } diff --git a/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution_test.go b/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution_test.go index 14ff50eb4..88faf8b88 100644 --- a/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution_test.go +++ b/leetcode/2201-2300/2226.Maximum-Candies-Allocated-to-K-Children/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + candies []int + k int64 + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{5, 8, 6}, 3, 5}, + {"TestCase2", []int{2, 5}, 11, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.candies, 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.candies, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }