Alice is a kindergarten teacher. She wants to give some candies to the children in her class.  All the children sit in a line and each of them has a rating score according to his or her performance in the class.  Alice wants to give at least 1 candy to each child. If two children sit next to each other, then the one with the higher rating must get more candies. Alice wants to minimize the total number of candies she must buy. Example arr =  [ 4, 6, 4, 5 , 6 , 2 ] She gives the students candy in the following minimal amounts: [ 1, 2, 1, 2,  3, 1 ] . She must buy a minimum of 10 candies. Function Description Complete the candies function in the editor below. candies has the following parameter(s): int n: the number of children in the class int arr[n]: the ratings of each student Returns int: the minimum number of candies Alice must buy Input Format The first line contains an integer, n , the size of arr. Each of the next n lines contains an integer arr[ i ] indicating the rating of the student at position i. Constraints 1  <=   n  <=  10^5 1  <=  arr[ i ]  <=  10^5 Sample Input 0 3 1 2 2 Sample Output 0 4

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter13: Decision Making Under Uncertainty
Section13.5: Bayes’ Rule And Decision Trees
Problem 7P
icon
Related questions
Question

Alice is a kindergarten teacher. She wants to give some candies to the children in her class.  All the children sit in a line and each of them has a rating score according to his or her performance in the class.  Alice wants to give at least 1 candy to each child. If two children sit next to each other, then the one with the higher rating must get more candies. Alice wants to minimize the total number of candies she must buy.

Example

arr =  [ 4, 6, 4, 5 , 6 , 2 ]

She gives the students candy in the following minimal amounts: [ 1, 2, 1, 2,  3, 1 ] . She must buy a minimum of 10 candies.

Function Description

Complete the candies function in the editor below.

candies has the following parameter(s):

int n: the number of children in the class
int arr[n]: the ratings of each student
Returns

int: the minimum number of candies Alice must buy
Input Format

The first line contains an integer, n , the size of arr.
Each of the next n lines contains an integer arr[ i ] indicating the rating of the student at position i.

Constraints

1  <=   n  <=  10^5
1  <=  arr[ i ]  <=  10^5


Sample Input 0

3
1
2
2
Sample Output 0

4

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Probability Problems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole