There are n
children standing in a line. Each child is assigned a rating value given in the integer array ratings
.
You are giving candies to these children subjected to the following requirements:
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input: ratings = [1,0,2] Output: 5 Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: ratings = [1,2,2] Output: 4 Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively. The third child gets 1 candy because it satisfies the above two conditions.
Constraints:
n == ratings.length
1 <= n <= 2 * 104
0 <= ratings[i] <= 2 * 104
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 to the candy problem involves trying every single possible candy assignment. We start by giving each child the minimum amount of candy and then methodically explore every possible way to adjust those amounts.
Here's how the algorithm would work step-by-step:
def candy_brute_force(ratings):
number_of_children = len(ratings)
if number_of_children <= 0:
return 0
min_candies = float('inf')
def is_valid_distribution(candies):
for index in range(number_of_children):
# Check left neighbor
if index > 0 and ratings[index] > ratings[index - 1] and candies[index] <= candies[index - 1]:
return False
# Check right neighbor
if index < number_of_children - 1 and ratings[index] > ratings[index + 1] and candies[index] <= candies[index + 1]:
return False
return True
def generate_distributions(index, current_candies):
nonlocal min_candies
if index == number_of_children:
if is_valid_distribution(current_candies):
# Only update if its the minimum total number of candies so far
total_candies = sum(current_candies)
min_candies = min(min_candies, total_candies)
return
# Explore possibilities by adding 1 candy at a time
for candy_count in range(1, number_of_children * 5 + 1): # Iterate for all possible candy counts
generate_distributions(index + 1, current_candies + [candy_count])
generate_distributions(0, [])
return min_candies
This problem asks us to fairly distribute candies to children based on their ratings. The key insight is to ensure each child receives at least one candy and that children with higher ratings get more candies than their immediate neighbors. We can achieve this efficiently by traversing the line of children twice: once from left to right and then from right to left, making local adjustments to the candy distribution.
Here's how the algorithm would work step-by-step:
def candy(ratings):
number_of_children = len(ratings)
candies = [1] * number_of_children
# Ensure left neighbors with higher ratings get more candy
for i in range(1, number_of_children):
if ratings[i] > ratings[i - 1]:
candies[i] = candies[i - 1] + 1
# Ensure right neighbors with higher ratings get more candy
for i in range(number_of_children - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
candies[i] = max(candies[i], candies[i + 1] + 1)
total_candies = sum(candies)
return total_candies
Case | How to Handle |
---|---|
Empty or null input array | Return 0 because there are no children to distribute candies to. |
Array with only one child | Return 1 since only one candy is needed. |
Array with two children, ratings are equal | Return 2, each child gets one candy. |
Array with two children, one rating higher than other | Return 3; one child gets 1 candy, other gets 2. |
Array with all children having the same rating | Each child receives exactly one candy, return the array's length. |
Array with monotonically increasing ratings | The candies should increment from 1 to n, so the sum of integers from 1 to n will be the result. |
Array with monotonically decreasing ratings | The candies should decrement from n to 1, so the sum of integers from 1 to n will be the result. |
Array with a V-shaped rating pattern (one child is lowest rated) | The algorithm correctly handles this, since it iterates both left to right and right to left. |