Python simple 10-line two passes solution with explanation
Scan the ratings forward, comparing ratings[i] to ratings[i-1]. candies[i] = candies[i-1] + 1 if rating is larger than the previous neighbor. After this pass, w
1. Scan the ratings forward, comparing ratings[i] to ratings[i-1]. candies[i] = candies[i-1] + 1 if rating is larger than the previous neighbor. After this pass, we know each child gets more candies to its left neighbor if the rating is higher.\n2. Scan the ratings backward, comparing ratings[i] to ratings[i+1]. Update candies if candies[i+1] > candies[i] and ratings[i] < ratings[i+1]. Because we have detected an inversion in the number of candies, we have to increment candies[i] to candies[i+1] + 1. After the second pass, we maintain the order from first pass and also make sure each child gets more candies to its right neighbor if the rating is higher. \n3. Return total candies\n```\nclass Solution(object):\n    def candy(self, ratings):\n        """\n        :type ratings: List[int]\n        :rtype: int\n        """\n        if ratings == []:\n            return 0\n        \n        candies = [1] * len(ratings)\n        \n        for i in range(1, len(ratings)):\n            if ratings[i] > ratings[i-1]:\n                candies[i] = candies[i-1] + 1\n                \n        for i in range(len(ratings)-2, -1, -1):\n            if ratings[i] > ratings[i+1] and candies[i] <= candies[i+1]:\n                candies[i] = candies[i+1] + 1\n        \n        return sum(candies)\n```
[]