Candies

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
32 upvotes
Asked in companies
Goldman SachsOYOMorgan Stanley

Problem statement

Prateek is a kindergarten teacher. He wants to give some candies to the children in his class. All the children stand in a line and each of them has a grade according to his or her performance in the class. Prateek wants to give at least one candy to each child. If two children are standing adjacent to each other, then the one with the higher rating must get more candies than the other. Prateek wants to minimize the total number of candies he must buy.

Given an array 'STUDENTS' of size 'N' that contains the grades for each student, your task is to find what is the minimum number of candies Prateek must buy so that he can distribute them among his students according to the criteria given above.

Example :

Given students' ratings : [5, 8, 1, 5, 9, 4]. 
He gives the students candy in the following minimal amounts : [1, 2, 1, 2, 3, 1]. He must buy a minimum of 10 candies.

Note :

1. If two students having the same grade are standing next to each other, they may receive the same number of candies.
2. Every student must get at least a candy.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.

The first line of each test case contains an integer ‘N’ representing the number of students.

The second line of each test case contains 'N' space-separated integers representing the grades of each student.
Output Format :
For each test case, print the minimum number of candies required.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10^2
1 <= N <= 10^4
1 <= STUDENTS[i] <= 10^5

Time Limit : 1 sec
Sample Input 1 :
3
2
1 5
3
1 3 4
3
1 2 2
Sample Output 1 :
3
6
4
Explanation For Sample Input 1 :
(i) Optimal distribution will be 1 2
(ii) Optimal distribution will be 1 2 3
(iii) Optimal distribution will be 1 2 1 because for children with equal grades one child can have less candies
Sample Input 2 :
3
1
100
5
1 5 3 4 6
6
1 9 1 3 2 4
Sample Output 2 :
1
9
9
Explanation For Sample Input 2 :
(i) Optimal distribution will be 1
(ii) Optimal distribution will be 1 2 1 2 3
(iii) Optimal distribution will be 1 2 1 2 1 2
Hint

Try to check for each element of the array sequentially and give each child candy according to the grades of previous and next child.

Approaches (2)
Brute Force

There can be four cases :

 

CASE 1: STUDENTS[i - 1]  > STUDENTS[i]  < STUDENTS[i + 1]

CASE 2: STUDENTS[i - 1]  < STUDENTS[i]  < STUDENTS[i + 1]

CASE 3: STUDENTS[i - 1]  > STUDENTS[i]  > STUDENTS[i + 1]

CASE 4: STUDENTS[i - 1]  < STUDENTS[i]  > STUDENTS[i + 1]

 

  1. For case 1, the ‘i’th child will get one candy.
  2. For case 2, the ‘i’th child will get ‘CANDIES’[i - 1] + 1 candies.
  3. For case 3, the ‘i’th child will get 'CANDIES'[i + 1] + 1 candies.
  4. For case 4, the ‘i’th child will get MAX('CANDIES'[i - 1],'CANDIES'[i + 1]) + 1 candies.

 

Here is the algorithm :

 

  1. Create an array (say, ‘CANDIES’) of size ‘N’.
  2. Run a loop from 0 to ‘N’ (say, iterator ‘i’) over ‘STUDENTS’[i] :
    • If ‘STUDENTS’[i -1] ≥ ‘STUDENTS’[i] and ‘STUDENTS’[i] ≤ ‘STUDENTS’[i + 1], do ‘CANDIES’[i] = 1.
  3. Run a loop from 0 to ‘N’ (say, iterator ‘i’) over ‘STUDENTS’[i] :
    • If ‘STUDENTS’[i - 1] < ‘STUDENTS’[i] and ‘STUDENTS’[i] < ‘STUDENTS’[i + 1], do ‘CANDIES’[i] = ‘CANDIES’[i - 1] + 1.
  4. Run a loop from ‘N’ - 1 to 0 (say, iterator ‘i’) over ‘STUDENTS’[i] :
    • If ‘STUDENTS’[i - 1] ≥ ‘STUDENTS’[i] and ‘STUDENTS’[i] ≥ ‘STUDENTS’[i + 1], do ‘CANDIES’[i] = ‘CANDIES’[i + 1] + 1.
  5. Run a loop from 0 to ‘N' (say, iterator ‘i’) over ‘STUDENTS’[i] :
    • If ‘STUDENTS’[i - 1] < ‘STUDENTS’[i] and ‘STUDENTS’[i] ≥ ‘STUDENTS’[i + 1], do ‘CANDIES’[i] = MAX('CANDIES'[i - 1], ‘CANDIES’[i + 1]) + 1.
  6. Create a variable (say, ‘ANS’) and initialise it to 0.
  7. Run a loop from 0 to ‘N’ (say, iterator ‘i’) add ‘CANDIES’[i] to ‘ANS’.
  8. Finally, return ‘ANS’.
Time Complexity

O(N), where ‘N’ is the size of the array.

 

We traverse the many times individually to calculate the number of candies. Hence, the overall time complexity will be O(N).

Space Complexity

O(N), where ‘N’ is the size of the array.

 

We use an array of size ‘N’ to store the candies for the students. Hence, the overall space complexity will be O(N).

Code Solution
(100% EXP penalty)
Candies
Full screen
Console