Python | 99.82% | Beginner Friendly | Optimal Solution
Python | 99.82% | Beginner Friendly | Optimal Solution\n\nclass Solution:\n    def candy(self, R):\n        n, ans = len(R), [1]*len(R)\n        \n        for i
# Python | 99.82% | Beginner Friendly | Optimal Solution\n```\nclass Solution:\n    def candy(self, R):\n        n, ans = len(R), [1]*len(R)\n        \n        for i in range(n-1):\n            if R[i] < R[i+1]:\n                ans[i+1] = max(1 + ans[i], ans[i+1])\n                \n        for i in range(n-2, -1, -1):\n            if R[i+1] < R[i]:\n                ans[i] = max(1 + ans[i+1], ans[i])\n        \n        return sum(ans)\n```
['Python', 'Python3']