#2226
Maximum Candies Allocated to K Children
specialist · 870 · lc medium +31 · verified · 49.9% accepted · 1,820 likes · top 37%
Description
You are given a 0-indexed integer array candies where each element is a candy pile. Piles may be split into smaller sub-piles but may not be merged.
You are also given an integer k. Allocate sub-piles to k children so that each child receives the same number of candies. Each child can only receive candies from one pile, and some piles may go unused.
Return the maximum number of candies each child can receive.
Example 1:
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:
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.
Code
1
2
3