|
| 1 | +# 0004 Candy ( L-A ) |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings. |
| 6 | + |
| 7 | +You are giving candies to these children subjected to the following requirements: |
| 8 | +- Each child must have at least one candy. |
| 9 | +- Children with a higher rating get more candies than their neighbors. |
| 10 | + |
| 11 | +Return the minimum number of candies you need to have to distribute the candies to the children. |
| 12 | + |
| 13 | +## Test Case |
| 14 | + |
| 15 | +```javascript |
| 16 | +Input: ratings = [1,0,2] |
| 17 | +Output: 5 |
| 18 | +Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively. |
| 19 | +``` |
| 20 | + |
| 21 | +## Solution |
| 22 | + |
| 23 | +```javascript |
| 24 | +var candy = function(ratings) { |
| 25 | + const candies = ratings.map(() => 1); |
| 26 | + const len = ratings.length; |
| 27 | + if (len <= 1) return len; |
| 28 | + |
| 29 | + for (let index = 1; index < len; index++) { |
| 30 | + if (ratings[index] > ratings[index-1]) { |
| 31 | + candies[index] = candies[index-1] + 1; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + let sum = candies[len-1]; |
| 36 | + for (let index = len-2; index >= 0; index--) { |
| 37 | + if (ratings[index] > ratings[index+1] && candies[index] <= candies[index+1]) { |
| 38 | + candies[index] = candies[index+1] + 1; |
| 39 | + } |
| 40 | + sum += candies[index]; |
| 41 | + } |
| 42 | + |
| 43 | + return sum; |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +## How it works |
| 48 | + |
| 49 | +- Given an array of integers representing ratings of candy, the goal is to distribute minimum candies among children such that a child with a higher rating gets more candies than their neighbor with a lower rating. |
| 50 | +- The code first initializes a new array called `candies` with the same length as the input `ratings` array, and fills it with ones. This means that initially, each child will receive at least one candy. |
| 51 | +- Next, the code checks if the input `ratings` array has a length of 1 or less, in which case the function simply returns the length of the array. |
| 52 | +- Then, the code loops through the input `ratings` array, starting from the second element, and compares each element to the previous element. If the current element has a higher rating than the previous element, the corresponding element in the `candies` array is updated to be one more than the previous element's value. This ensures that children with higher ratings receive more candies than their neighbors with lower ratings. |
| 53 | +- After that, the code loops through the `candies` array again, this time starting from the second-to-last element and going backwards. For each element, the code checks if it has a higher rating than its next neighbor and if its current number of candies is less than or equal to its next neighbor's number of candies. If both conditions are true, the current element's number of candies is updated to be one more than its next neighbor's number of candies. This makes sure that the distribution of candies is optimal and the minimum number of candies are used. |
| 54 | +- Finally, the code calculates the total number of candies distributed by summing up all the elements in the `candies` array and returns the sum. |
| 55 | + |
| 56 | +## References |
| 57 | + |
| 58 | +- [LeetCode](https://leetcode.com/problems/candy/) |
| 59 | + |
| 60 | +## Problem Added By |
| 61 | + |
| 62 | +- [Haris](https://github.com/harisdev-netizen) |
| 63 | + |
| 64 | +## Contributing |
| 65 | + |
| 66 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 67 | + |
| 68 | +Please make sure to update tests as appropriate. |
0 commit comments