Maximum Candies Allocated to K Children
Problem
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.
Examples
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.
Constraints:
1 <= candies.length <= 10^51 <= candies[i] <= 10^71 <= k <= 10^12
Solution
Method 1 - Using Binary Search
The problem involves distributing piles of candies to k children such that each child receives the same number of candies from a single pile. Since we can split the piles but cannot merge them, the objective is to find the maximum number x such that at least k piles of size x can be created. To solve this efficiently:
Approach
-
Binary Search:
- Use binary search to determine the maximum possible number
xof candies each child can receive. - Searching is performed in the range from
1tomax(candies), wheremax(candies)is the largest pile size. - For each candidate
x, check if it is possible (feasible) to distribute the candies such that at leastkchildren receive a pile of sizexor larger.
- Use binary search to determine the maximum possible number
-
Feasibility Check:
- For a given candidate
x, calculate how many children can be served. This is done by summing uppile // xfor all piles in thecandiesarray. - If the total number of children served is greater than or equal to
k, thenxis feasible.
- For a given candidate
-
Binary Search Adjustment:
- If
xis feasible, search for a largerx(right side of the binary search range). - Otherwise, search for a smaller
x(left side of the binary search range).
- If
Code
Java
class Solution {
public int maximumCandies(int[] candies, long k) {
// If the total candies in all piles are not enough for k children, return 0.
long totalCandies = 0;
for (int c : candies) {
totalCandies += c;
}
if (totalCandies < k) return 0;
int low = 1, high = Integer.MAX_VALUE, ans = 0;
while (low <= high) {
int mid = low + (high - low) / 2; // Calculate mid-point
if (canDistribute(candies, k, mid)) {
ans = mid; // If feasible, update ans and move to the larger range
low = mid + 1;
} else {
high = mid - 1; // Otherwise, move to the smaller range
}
}
return ans;
}
private boolean canDistribute(int[] candies, long k, int x) {
long count = 0; // Count of children who can get at least x candies
for (int c : candies) {
count += c / x; // Calculate number of children that can be served from each pile
if (count >= k) return true; // Stop early if we can serve k children
}
return false; // Cannot serve k children with current x
}
}
Python
class Solution:
def maximumCandies(self, candies: List[int], k: int) -> int:
def can_distribute(x: int) -> bool:
# Check if it is possible to distribute 'x' candies to at least 'k' children
count = sum(c // x for c in candies)
return count >= k
total_candies = sum(candies)
if total_candies < k:
return 0 # Not enough candies for 'k' children
low, high, ans = 1, max(candies), 0
while low <= high:
mid = (low + high) // 2 # Middle value in binary search
if can_distribute(mid): # Check feasibility of current 'mid'
ans = mid # Update result if feasible
low = mid + 1 # Look for larger values
else:
high = mid - 1 # Look for smaller values
return ans # Maximum candies each child can receive
Complexity
- ⏰ Time complexity:
O(n*log(M)), wherenis the size of array andMis the max number of candles.- Binary search operates over the range
[1, max(candies)]with aroundlog(max(candies))iterations. - For each iteration, the feasibility check scans the
candiesarray (sizen) inO(n)time. - Total time complexity is
O(n * log(max(candies))).
- Binary search operates over the range
- 🧺 Space complexity:
O(1)