NeetCode #278LC-2226MediumBinary Search
← Back to All Problems#278 · #2226 · Maximum Candies Allocated to K Children(分配给 K 个孩子的最大糖果数)
📌 Problem Statement & Constraints
You are given a 0-indexed integer array
candies where candies[i] is the number of candies in the i-th pile, and an integer k. Each child receives candies from a single pile, and piles cannot be split across children. Return the maximum number of candies each child can get. Constraints: 1 <= candies.length <= 10^5, 1 <= candies[i] <= 10^7, 1 <= k <= 10^12.💡 Core Algorithmic Approaches
- Binary search on the answer: "can each of the
kchildren get at leastccandies" is monotone inc. - For a given
c, the number of children that can be served issum(pile // c). - The answer is feasible when that count is at least
k. Use the upper-biased midpoint to maximise. - If the total number of candies is less than
k, the answer is 0 -- check this before the search.
💻 Benchmark Python3 Implementation
class Solution:
def maximumCandies(self, candies: List[int], k: int) -> int:
if sum(candies) < k:
return 0 # impossible to give everyone one
lo, hi = 1, max(candies)
while lo < hi:
mid = (lo + hi + 1) // 2 # upper-biased: maximise
children = sum(c // mid for c in candies)
if children >= k:
lo = mid # feasible -> try larger
else:
hi = mid - 1
return lo⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n log(max(candies))): each predicate is O(n).
💾 Space Complexity
O(1): a few scalars.
⚠️ Interview Pitfalls & Follow-ups
- Using the lower-biased midpoint with
lo = mid: infinite loop; the upper bias is required for maximisation. - Forgetting the feasibility check: if the total is less than
k, the binary search would still return 1 (sincemid = 1always givessum(candies) >= k... which is false), so the guard is needed. - Allowing a pile to be split between children: each child takes from exactly one pile, which is why the floor division is used.
- Using
children == kinstead of>= k: serving more children than required is fine; the surplus is simply unused.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Binary Search Problems
中文速览与面试要点
分配给 K 个孩子的最大糖果数
给你一个下标从 0 开始的整数数组 candies 和一个整数 k。你可以将 candies 中的每个元素分配到 k 个孩子中的一个,每个孩子 最多 只能得到一堆糖果,且每堆糖果的数量必须 相同 。请你返回每个孩子能得到的 最大 糖果数量。如果无法分配,返回 0。约束:1 ≤ candies.length ≤ 10^5;1 ≤ candies[i] ≤ 10^7;1 ≤ k ≤ 10^12。
核心要点
- 二分答案(标准解) :二分「每个孩子分到的糖果数 x」,判定「所有堆能分出至少 k 份大小为 x 的糖果」。
- 单调性 :x 越小越容易满足,因此可以二分(求最大值)。
- 判定函数 : sum(c // x for c in candies) >= k 。每堆最多分出 c // x 份。
- 边界 :若 sum(candies) < k 则无法分配,返回 0(也可由二分的上界 max(candies) 与判定函数自然处理,但显式剪枝更快)。
- 二分模板 :求最大值,用向上取整的 mid 配合 l = mid 。
常见陷阱
- 二分模板用向下取整的 mid 配合 l = mid :会死循环。求最大值必须用 (l + r + 1) // 2 。
- 判定条件写成 cnt == k :可能分出多于 k 份(多余的糖果不用),应判断 >= k 。
- 忘记 sum(candies) < k 的剪枝 :虽然二分会返回 0(因为判定始终失败,r 会降到 0),但显式剪枝更清晰。
- 用 sum(candies) // k 作为答案 :这是「平均分配」的结果,但每堆糖果不能拆分,实际答案可能更小。必须二分。
- k 可达 10^12 :判定函数中的 cnt 可能超出 int,Python 无此问题,C++/Java 需用 long long。
参考实现
class Solution:
def maximumCandies(self, candies: list[int], k: int) -> int:
if sum(candies) < k:
return 0 # 糖果总数不足
l, r = 1, max(candies)
while l < r:
mid = (l + r + 1) // 2 # 向上取整,配合 l = mid
cnt = 0
for c in candies:
cnt += c // mid # 每堆最多分出 c // mid 份
if cnt >= k:
l = mid # 可行 -> 尝试更大
else:
r = mid - 1
return l