114. Candy

HardArrayArray

There are n children standing in a line. Each child has a rating value. You must give at least 1 candy per child. Children with higher ratings than adjacent neighbors must get more candies. Return the minimum total candies.

Input: Integer array ratings of length n.

Output: Integer — minimum total candies.

Examples

Example 1
Input: [1,0,2]
Output: 5
Explanation: [2,1,2]. Total=5.
Example 2
Input: [1,2,2]
Output: 4
Explanation: [1,2,1]. Total=4.
Example 3
Input: [1,2,3,4,5]
Output: 15
Explanation: [1,2,3,4,5]. Total=15.

Constraints

Asked by

BloombergAmazonMicrosoftGoogleInfosysFlipkart
Solve this problem in the editor →