C++ Solution || O(n) || Easy Understanding
\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int n = ratings.size();\n        vector<int>left(n, 1), right(n, 1);\n        \n    
```\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int n = ratings.size();\n        vector<int>left(n, 1), right(n, 1);\n        \n        //traverse left to right and compare curr value with left side value\n        for(int i = 1; i < n; i++)\n            if(ratings[i] > ratings[i-1]) left[i] = left[i-1]+1;\n              \n         //traverse right to left and compare curr value with right side value\n        for(int i = n-2; i >= 0; i--)\n            if(ratings[i] > ratings[i+1]) right[i] = right[i+1]+1;\n        \n        int ans = 0;\n        for(int i = 0; i < n; i++)\n            ans += max(left[i], right[i]);\n        \n        return ans;            \n    }\n};\n```
['C', 'C++']