Playing Hacks and Stuffs!
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.
From the problem description, we want to find the minimum number of candies needed to satisfy those requirements while also ensuring that children with higher ratings get more candies than their neighbors.
Let’s take the first test case example:
Suppose you have the following ratings for children:
ratings = [1,0,2]
This means that:
Child 0 has a rating of 1.
Child 1 has a rating of 0.
Child 2 has a rating of 2
To satisfy the conditions:
[1, 1, 1]
.Child 2
has a higher rating than its neighbors (1 and 0)
, so we give it more candies than its neighbors. The updated distribution becomes [1, 1, 2]
.Now, we’ve met both conditions. Child 0
has one candy, Child 1
has one candy, and Child 2
has two candies. The minimum number of candies needed to satisfy these conditions is 1 + 1 + 2 = 4
candies.
The goal of the problem is to find the minimum total number of candies needed to satisfy these conditions for any given set of ratings.
In my solve script I basically iterate twice and one the first iteration I check for the children with higher ratings so as to give them more candies than their left neighbors, and the second iterate I check for the children with higher ratings so as to give them more candies than their right neighbors
From there the minimum total number of candies needed is then calculated based on this distribution.
Here’s my solve script: link