Candy - Complete Solution Guide
Candy is LeetCode problem 135, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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: Each child must have at least one candy. Children with a higher rating get more candies than their neighbors. 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, seco
Detailed Explanation
The "Candy" problem requires us to distribute candies to a line of children, where each child has a rating. The distribution must satisfy two conditions: every child receives at least one candy, and children with higher ratings than their neighbors receive more candies than their neighbors. The goal is to minimize the total number of candies required.
Solution Approach
The solution uses a greedy approach, making local optimal choices to arrive at the global minimum. It involves initializing a `candies` array where each child initially receives one candy. Then, it performs two passes: one from left to right to satisfy the increasing rating condition with the left neighbor, and another from right to left to satisfy the increasing rating condition with the right neighbor. During the second pass, we use `max()` to ensure the left-to-right candy count is preserved when applicable, to handle scenarios where the initial count based on the left neighbor is better for the overall min candy distribution.
Step-by-Step Algorithm
- Step 1: Initialize an array `candies` of the same size as the `ratings` array, with each element set to 1. This ensures each child initially receives at least one candy.
- Step 2: Iterate through the `ratings` array from left to right (from index 1 to n-1). If a child's rating is higher than the rating of the child to its left, update the child's candy count to be one more than the candy count of the child to its left: `candies[i] = candies[i-1] + 1`.
- Step 3: Iterate through the `ratings` array from right to left (from index n-2 to 0). If a child's rating is higher than the rating of the child to its right, update the child's candy count to be the maximum of its current candy count and one more than the candy count of the child to its right: `candies[i] = max(candies[i], candies[i+1] + 1)`.
- Step 4: Calculate the sum of all the elements in the `candies` array. This sum represents the minimum number of candies required to satisfy the given conditions.
- Step 5: Return the calculated sum.
Key Insights
- Insight 1: The problem can be solved by making two passes through the ratings array: one from left to right and another from right to left. This ensures that both neighbors are considered when distributing candies.
- Insight 2: Each child's candy count depends on its immediate neighbors. We can initialize each child with one candy and then adjust the counts based on their neighbors' ratings during each pass.
- Insight 3: The second pass needs to consider the candy counts already assigned in the first pass. If a child has a higher rating than its right neighbor but already has fewer or the same candies, its candy count needs to be updated.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Greedy.
Companies
Asked at: Accenture, ConsultAdd, Flipkart, Goldman Sachs, Komprise, Mastercard, PhonePe, Tencent, Teradata, Uber, Urban Company, Yandex.