We’re preparing your current view and syncing the latest data.
You are given an integer array ratings representing the ratings of children standing in a line. You need to distribute candies to these children subject to the following requirements:
Return the minimum number of candies you need to have to distribute according to the above rules.
An integer array ratings where ratings[i] represents the rating of the ith child.
An integer representing the minimum number of candies required to distribute according to the rules.
1 <= ratings.length <= 2 * 10^4 0 <= ratings[i] <= 2 * 10^4
Example 1
Input
[1,0,2]
Output
5
Explanation
Children with ratings [1,0,2] can be given candies [2,1,2], which satisfies the conditions.
Example 2
Input
[1,2,2]
Output
4
Explanation
Children with ratings [1,2,2] can be given candies [1,2,1] as the last two children have equal ratings, so the last child does not require more candies than the second.