Python || Two pass || explanation || intuition || greedy
Upvote if u like\n1. Traverse once right to left and later left to right\n2. temp contains candy counts , with initially all having one candy\n3. In right to le
**Upvote if u like**\n1. Traverse once right to left and later left to right\n2. temp contains candy counts , with initially all having one candy\n3. In right to left traversal, if ratings[i]>ratings[i-1] , give one candy to i th child.\n4. We traverse backward for testcases like [1,3,2,2,1] here before backward traversal temp is [1,2,1,1,1] , but real ans should be [1,2,1,2,1]. So for this we traverse backward.\n\n\n```\nclass Solution:\n    def candy(self, ratings: List[int]) -> int:\n        n=len(ratings)\n        temp = [1]*n\n        \n        for i in range(1,n):\n            if(ratings[i]>ratings[i-1]):\n                temp[i]=temp[i-1]+1\n        if(n>1):\n            if(ratings[0]>ratings[1]):\n                temp[0]=2\n                \n            \n        for i in range(n-2,-1,-1):\n            if(ratings[i]>ratings[i+1] and temp[i]<=temp[i+1]):\n                temp[i]=temp[i+1]+1\n\n                \n        return sum(temp)\n```\n
['Greedy', 'Python', 'Python3']