You are given two positive integers n
and limit
.
Return the total number of ways to distribute n
candies among 3
children such that no child gets more than limit
candies.
Example 1:
Input: n = 5, limit = 2 Output: 3 Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
Example 2:
Input: n = 3, limit = 3 Output: 10 Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
Constraints:
1 <= n <= 106
1 <= limit <= 106
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for distributing candies is like trying every single possible way to give candies to the children. We start by giving the minimum amount to everyone and then explore all combinations of giving out the remaining candies one by one.
Here's how the algorithm would work step-by-step:
def distribute_candies_brute_force(number_of_candies,
number_of_children,
limit_candies_per_child):
candies_distribution = [0] * number_of_children
# Give each child the minimum amount of candies possible
candies_to_give = min(number_of_candies // number_of_children, limit_candies_per_child)
for child_index in range(number_of_children):
candies_distribution[child_index] = candies_to_give
remaining_candies = number_of_candies - (candies_to_give * number_of_children)
if remaining_candies == 0:
return candies_distribution
def backtrack(child_index, candies_left):
nonlocal candies_distribution
if candies_left == 0:
return sum(candies_distribution) == number_of_candies
if child_index == number_of_children:
return False
for candies_to_add in range(min(candies_left + 1,
limit_candies_per_child -
candies_distribution[child_index] + 1)):
candies_distribution[child_index] += candies_to_add
# Explore adding candies to this child
if backtrack(child_index + 1, candies_left - candies_to_add):
return True
candies_distribution[child_index] -= candies_to_add
return False
# Iterate through each child to distribute the remaining candies
if backtrack(0, remaining_candies):
return candies_distribution
#If no possible distribution is found
candies_distribution = [0] * number_of_children
candies_assigned = 0
for i in range(number_of_children):
candies_to_assign = min(limit_candies_per_child, number_of_candies - candies_assigned)
candies_distribution[i] = candies_to_assign
candies_assigned += candies_to_assign
return candies_distribution
The problem asks us to distribute candies to children in a specific pattern until we run out of candies. The key insight is to calculate how many rounds of distribution we can complete fully before candies become insufficient for a full round, and then handle the remaining candies separately.
Here's how the algorithm would work step-by-step:
def distribute_candies(number_of_children, total_candies):
candies_per_child = [0] * number_of_children
children_in_cycle = number_of_children
candies_for_full_cycle = (children_in_cycle * (children_in_cycle + 1)) // 2
# Calculate how many full cycles we can complete
number_of_full_cycles = 0
if candies_for_full_cycle > 0:
number_of_full_cycles = total_candies // candies_for_full_cycle
remaining_candies = total_candies - number_of_full_cycles * candies_for_full_cycle
# Distribute candies based on the full cycles
for child_index in range(number_of_children):
candies_per_child[child_index] = (child_index + 1) * number_of_full_cycles
# Distribute the remaining candies
child_index = 0
candy_to_give = number_of_full_cycles * 1 + 1
while remaining_candies > 0:
# Distribute candies until we run out
if remaining_candies >= candy_to_give:
candies_per_child[child_index] += candy_to_give
remaining_candies -= candy_to_give
candy_to_give += 1
else:
candies_per_child[child_index] += remaining_candies
remaining_candies = 0
child_index += 1
return candies_per_child
Case | How to Handle |
---|---|
n is zero (no candies) | If n is zero, return 1 if num_children is also zero or some combination of limit allows the children to have no candy, otherwise 0. |
num_children is zero | If num_children is zero and n is also zero return 1, if n is non zero there is no way to distribute and return 0. |
limit is zero | If limit is zero, return 1 only when n is also 0, and 0 otherwise. |
n is negative | If n is negative, it's an invalid input and should return 0 since you cannot distribute negative candies. |
limit is negative | If the limit is negative, it is an invalid input and should return 0 because a child can't have a negative candy limit. |
n is much larger than num_children * limit | This means distribution is impossible, return 0. |
Integer overflow in calculations | Use appropriate data types (e.g., long long in C++, long in Java) to prevent integer overflow during intermediate calculations, particularly when dealing with combinations. |
n equals num_children * limit | There is only one way which is when each children gets the maximum candy allowed, return 1. |