← 返回 bytedance 的题目列表Candy Distribution Problem
类型:online_judge
There are n children standing in a line. Each child is assigned a rating value. You are required to distribute candies to these children subject to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors. Determine the minimum number of candies you need to have to distribute the candies to the children.
Example: Input: ratings = [1, 0, 2] Output: 5 Explanation: You can allocate candies to the children like this: [2, 1, 2]
Input: ratings = [1, 2, 2] Output: 4 Explanation: You can allocate candies to the children like this: [1, 2, 1]
Example
Input
[1, 0, 2]