125. Candy Distribution

HardArrayArray

There are n children in a line each with a rating. Distribute candies so each child gets at least one, and any child with a higher rating than an immediate neighbor gets more candies than that neighbor. Return the minimum total candies.

Input: An integer array of ratings.

Output: Integer — minimum candies.

Examples

Example 1
Input: [1,0,2]
Output: 5
Explanation: Candies [2,1,2]=5.
Example 2
Input: [1,2,2]
Output: 4
Explanation: Candies [1,2,1]=4.
Example 3
Input: [1]
Output: 1
Explanation: One child.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →