[没人气,抄道题] 分糖果
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
n个小孩站一排,每个小孩有一个等级值。现在分发糖果给他们,要求:You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
a. 每个小孩至少分到一颗糖。
b. 等级高的小孩要比靠近他的低等级小孩拿到的糖果多。
请计算最少需要多少糖果
例如6个小孩,等级分别是 1, 3, 7, 5, 3, 2
那么糖果得这样分才最少:1, 2, 4, 3, 2, 1
即对于 { 1, 3, 7, 5, 3, 2 } 这个数列,结果为 13