O(N) solution with greedy
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int candy(vector<int>& r) {\n        int n=r.size();\n        vector<int>c(n,1);\n        //0 1 2 1 3\n        if(n==1)return 1;\n        if(r[0]>r[1])c[0]=2;\n        for(int i=1;i<n;i++)\n        {\n            if(r[i]>r[i-1] )\n            {\n                c[i]+=c[i-1];\n            }\n           \n        }\n        \n         for(int i=n-2;i>=0;i--)\n        {\n            if(r[i]>r[i+1] && c[i]<=c[i+1] )\n            {\n                c[i]=max(c[i],c[i+1]+1);\n            }\n           \n        }\n        int ans=0;\n        for(int i=0;i<n;i++)\n        {\n          ans+=c[i];\n         \n        }\n        return ans;\n    }\n};\n```
['C++']