AfterAcademy Tech
•
08 Aug 2020

Difficulty: Easy
Asked in: Amazon, Microsoft
Problem Description
There are N children standing in a line with some rating value. You want to distribute a minimum number of candies to these children such that:
You need to write a program to calculate the minimum candies you must give.
arr[] of N integers representing the ratings of each studentExample 1
Input: arr[] = [1, 2, 2]
Output: 4
Explanation: You can distribute to the first, second and third child 1, 2 and 1 candies respectively. The third child gets 1 candy because it satisfies the above two conditions.
Example 2
Input: arr[] = [1, 5, 2, 1]
Output: 7
Explanation: Candies given = [1, 3, 2, 1]
We will be discussing two different solutions to this problem:-
You may try this problem here.
The most straight forward approach for this problem would be to start with distributing one candy to each child and then check if any child who has more rating than his one of the neighbours should have more candy than its neighbour. So, distribute one more candy to that child who does not satisfy the above condition. Do this until every child is satisfied.
Solution Steps
Create a 1-D candy array that will represent the number of candies given to each child corresponding to the rating array. The question states that each child must have at least one candy so, initialize each index of candy array by one.
ith child rating is higher than (i-1)th child, then candy[i] should be greater than candy[i-1] . So, update candy[i] = candy[i-1]+1 .ith child rating is greater than (i+1)th child, then candy[i] should be greater than candy[i+1] . So, update candy[i] = candy[i+1] + 1 .Do the above two steps until no update in the number of candies required for any child.
Ultimately return the sum of candy array which will be the minimum number of candies required.
Pseudo Code
int candy(int[] ratings, int size) {
int[size] candies = {1}
bool flag = true
while (flag) {
flag = false
for (int i = 0 to i < size) {
if (i != size - 1 and ratings[i] > ratings[i + 1] and candies[i] <= candies[i + 1]) {
candies[i] = candies[i + 1] + 1
flag = true
}
if (i > 0 and ratings[i] > ratings[i - 1] and candies[i] <= candies[i - 1]) {
candies[i] = candies[i - 1] + 1
flag = true
}
}
}
int sum = 0
for (int i = 0 to i < size) {
sum += candies[i]
}
return sum
}
Complexity Analysis
Time Complexity: O(n²)
Space Complexity: O(n)
Critical ideas to Think!
rating[i] > rating [i-1] ?Let’s divide the problem condition “children with higher ratings will have more candies than their neighbours” into two parts.
child[i] who have a higher rating than child[i-1]child[i] who have a higher rating than child[i+1]For the above two problems, we can maintain two 1D arrays that will save the number of candies given to each child in each case.
Now, just taking the max value of each index of the two arrays will represent the minimum number of candies required by each child corresponding to the index.
Solution Steps
left2right and right2left, and initialize each index by 1.(Why?)rating[i] > rating[i-1] then update left2right[i] with the previous value + 1rating[i] > rating[i+1] then update right2left[i] with the previous value + 1left2right[i] and right2left[i]Pseudo Code
int candy(int[] ratings, int size) {
int sum = 0
int[size] left2right = {1}
int[size] right2left = {1}
for (int i = 1 to i < size) {
if (ratings[i] > ratings[i - 1]) {
left2right[i] = left2right[i - 1] + 1
}
}
for (int i = size - 2 to i >= 0) {
if (ratings[i] > ratings[i + 1]) {
right2left[i] = right2left[i + 1] + 1
}
}
for (int i = 0 to i < size) {
sum += max(left2right[i], right2left[i])
}
return sum
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Critical ideas to Think!
max(left2right[i], right2left[i]) for each i ?Answer: Yes, you will notice that after the second pass in the above approach, we are only maintaining the max of each index from the two arrays. We can simply remove the second array and can update the first array like this,
left2right[i] = max(left2right[i], left2right[i + 1] + 1)
whenever rating[i] > rating[i+1]
and at last, return the sum of left2right.

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
AfterAcademy Tech
This is an interview problem asked by companies like Amazon. This problem is based on Greedy Algorithm and is one of the very basic problem for understanding Greedy Algorithms.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon, Microsoft, Yahoo and Adobe. The problem is to design a stack that will support push(), pop(), top() and getMin() operation in constant time. We will be discussing two different ways to approach this problem.

AfterAcademy Tech
In this blog, we will learn about the classic reader-writer problem in Operating System. We will also see how to remove this problem in Operating System.

AfterAcademy Tech
In this blog, we will learn about the Producer-Consumer problem in Operating System and we will also learn how to solve this problem. It is used in multi-process synchronization.
