from __future__ import print_function # Time: O(n) # Space: O(n) # # 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? # import operator from functools import reduce class Solution: # @param ratings, a list of integer # @return an integer def candy(self, ratings): candies = [1 for _ in xrange(len(ratings))] for i in xrange(1, len(ratings)): if ratings[i] > ratings[i - 1]: candies[i] = candies[i - 1] + 1 for i in reversed(xrange(1, len(ratings))): if ratings[i - 1] > ratings[i] and candies[i - 1] <= candies[i]: candies[i - 1] = candies[i] + 1 return reduce(operator.add, candies) if __name__ == "__main__": result = Solution().candy([1, 2, 3, 2, 3, 5, 2, 5]) print(result)