[C++/Python/Java] Simple solution with explanation and sample TC | O(N)
------Please upvote! if you like the solution and explanation.  Please put your questions/doubts in the comments section below.  I will try my best to answer th
*------Please **upvote!** if you like the solution and explanation.  Please put your questions/doubts in the comments section below.  I will try my best to answer them.------*\n\n**EXPLANATION:**\n\nCreate a result array (**`candies`**) to store the candies for each person.  First go from left to right and increment candies by 1 of people whose rating is more than the left neighbor.\n\nThen, go from right to left and if **`ratings [i] > ratings [i+1]`** update **`candies [i]`** to max of **`candies [i], candies [i+1] + 1`**\n\nFinally return sum of candies [].\n\nTake sample example:\n`candies [] = [1, 1, 1, 1, 1, 1, 1]` : Initialize candies with 1 candy each \n`ratings [] = [1, 6, 10, 8, 7, 3, 2]`\n\n**After 1st iteration:** update candies while going left to right: **`if ratings [i] > ratings [i-1] ---> candies [i] = candies [i-1] + 1`**\n\n`ratings [] = [1, 6, 10, 8, 7, 3, 2]`\n`candies [] = [1, 2, 3, 1, 1, 1, 1]`\n\n**After 2nd iteration:** update candies while going right to left:**`if ratings [i] > ratings [i+1] ---> candies [i] = max (candies [i+1] + 1, candies [i])`**\n\n`ratings [] = [1, 6, 10, 8, 7, 3, 2]`\n`candies [] = [1, 2, 5, 4, 3, 2, 1]`\n\n**Sum = 18** ---> Answer\n\n**C++ Code:**\n\n```\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int n = ratings.size();\n        vector <int> candies (n, 1);\n        for (int i = 1; i < n; i++) {\n            if (ratings [i] > ratings [i-1])\n                candies [i] = candies [i-1] + 1;\n        }\n        int sum = candies [n-1];\n        for (int i = n-2; i >= 0; i--) {\n            if (ratings [i] > ratings [i+1])\n                candies [i] = max (candies [i], candies [i+1] + 1);\n            sum += candies [i];\n        }\n        return sum;\n    }\n};\n```\n\n**Python Code:**\n```\nclass Solution:\n    def candy(self, ratings: List[int]) -> int:\n        n = len (ratings)\n        candies = [1] * n\n        for i in range (1, n):\n            if ratings [i] > ratings [i-1]:\n                candies [i] = candies [i-1] + 1\n        for i in range (n-2, -1, -1):\n            if ratings [i] > ratings [i+1]:\n                candies [i] = max (candies [i], candies [i+1] + 1)\n        return sum (candies)\n```\n\n**Java Code:**\n\n```\nclass Solution {\n    public int candy(int[] ratings) {\n        int n = ratings.length;\n        int candies [] = new int [n];\n        Arrays.fill (candies, 1);\n        for (int i = 1; i < n; i++) {\n            if (ratings [i] > ratings [i-1])\n                candies [i] = candies [i-1] + 1;\n        }\n        int sum = candies [n-1];\n        for (int i = n-2; i >= 0; i--) {\n            if (ratings [i] > ratings [i+1])\n                candies [i] = Math.max (candies [i], candies [i+1] + 1);\n            sum += candies [i];\n        }\n        return sum;\n    }\n}\n```\n\nPlease **upvote!** if you like the solution and explanation.
['Dynamic Programming', 'C', 'Python', 'Java']