C++ || O(n) Time , O(n) Space || easy brute force solution || with explanation
traverse towards towards left-right then towards right-left\n 2. if not understood why dry run this testcase once 1 2 87 87 87 2 1\n\nint candy(vector<int>& rat
1. traverse towards towards left-right then towards right-left\n 2. if not understood why dry run this testcase once 1 2 87 87 87 2 1\n```\nint candy(vector<int>& ratings) {\n        int n=ratings.size();\n        if(n==1) return 1;\n        vector<int> ans(n,1);\n        //firsly traverse owards right\n        for(int i=0;i<n;i++)\n        {\n            if(i==0 && ratings[i]>ratings[i+1])\n            {\n                ans[i]=ans[i+1]+1;\n            }\n            else if(i>0 && i<n-1)\n            {\n                if(ratings[i]>ratings[i-1] && ratings[i]>ratings[i+1])\n                {\n                    ans[i]=max(ans[i+1],ans[i-1])+1;\n                }\n                else if(ratings[i]>ratings[i-1])\n                {\n                    ans[i]=ans[i-1]+1;\n                }\n                else if(ratings[i]>ratings[i+1] )\n                {\n                    ans[i]=ans[i+1]+1;\n                }\n            }\n            else if(i==n-1)\n            {\n                if(ratings[i]>ratings[i-1])\n                {\n                    ans[i]=ans[i-1]+1;\n                }\n            }\n        }\n        \n\t\t//then traverse towards left\n\t\t\n        for(int i=n-1;i>=0;i--)\n        {\n            if(i==0 && ratings[i]>ratings[i+1])\n            {\n                ans[i]=ans[i+1]+1;\n            }\n            else if(i>0 && i<n-1)\n            {\n                if(ratings[i]>ratings[i-1] && ratings[i]>ratings[i+1])\n                {\n                    ans[i]=max(ans[i+1],ans[i-1])+1;\n                }\n                else if(ratings[i]>ratings[i-1])\n                {\n                    ans[i]=ans[i-1]+1;\n                }\n                else if(ratings[i]>ratings[i+1] )\n                {\n                    ans[i]=ans[i+1]+1;\n                }\n            }\n            else if(i==n-1)\n            {\n                if(ratings[i]>ratings[i-1])\n                {\n                    ans[i]=ans[i-1]+1;\n                }\n            }\n        }\n       \n       \n        int res=accumulate(ans.begin(),ans.end(),0);\n        return res;\n    }\n\t\t```\n\t\tif you have reached there then you would have probably understood the solution , So\n\t\t**upvote please**\n\t\t
['Array']