Candy [Python]  144ms runtime 2 solutions
\n\n\nclass Solution:\n    def candy(self, ratings: List[int]) -> int:\n        lenratings = len(ratings)       # call len only once. It is used 3 times\n      
\n\n```\nclass Solution:\n    def candy(self, ratings: List[int]) -> int:\n        lenratings = len(ratings)       # call len only once. It is used 3 times\n        ans = [1] * lenratings\n        for i in range(1, lenratings):\n            if ratings[i] > ratings[i-1]:\n                ans[i] = ans[i-1] + 1\n        for i in range(lenratings-2, -1, -1):\n            a = i+1                     # a is used 2 times in if\n            # compare rantings and candys is faster than use "max" to calculate.\n            if ratings[i] > ratings[a] and ans[i] <= ans[a]:\n                ans[i] = ans[a] + 1\n        return sum(ans)\n```\n\nIn this second solution I try to save some time in the reverse loop saving only indexes that should be visited. But in the end of the tests the runtime is the same but usage memory is bigger.\n\n```\nclass Solution:\n    def candy(self, ratings: list) -> int:\n        lenratings = len(ratings)       # call len only once. It is used 3 times\n        ans = [1] * lenratings\n        b = []\n        for i in range(1, lenratings):\n            if ratings[i] > ratings[i-1]:\n                ans[i] = ans[i-1] + 1\n            else:\n                b.append(i-1)\n        b.reverse()\n        for i in b:\n            a = i+1                     # a is used 2 times in if\n            # compare rantings and candys is faster than use "max" to calculate.\n            if ratings[i] > ratings[a] and ans[i] <= ans[a]:\n                ans[i] = ans[a] + 1\n        return sum(ans)\n```\n\n
['Python3']