Simple python solution with two passes
class Solution:\n        # @param {integer[]} ratings\n        # @return {integer}\n        def candy(self, ratings):\n            # use two pass scan from left
    class Solution:\n        # @param {integer[]} ratings\n        # @return {integer}\n        def candy(self, ratings):\n            # use two pass scan from left to right and vice versa to keep the candy level up to now\n            # similar to like the Trapping Rain Water question\n            res = [1]*len(ratings) # also compatable with [] input\n            lbase = rbase = 1\n            # left scan\n            for i in xrange(1, len(ratings)):\n                lbase = lbase + 1 if ratings[i] > ratings[i-1] else 1\n                res[i] = lbase\n            # right scan\n            for i in xrange(len(ratings)-2, -1, -1):\n                rbase = rbase + 1 if ratings[i] > ratings[i+1] else 1\n                res[i] = max(rbase, res[i])\n            return sum(res)
['Greedy']