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?
class Solution:
# @param ratings, a list of integer
# @return an integer
def candy(self, ratings):
mark = [1 for i in range(len(ratings))]
# left to right
i=1
while i < len(mark):
if ratings[i] > ratings[i-1]:
mark[i]=mark[i-1]+1
i=i+1
#right to left
i=len(mark)-2
while i>=0:
if ratings[i] > ratings[i+1]:
mark[i]=max(mark[i+1]+1,mark[i])
i=i-1
return sum(mark)