✅💯🔥Simple Code🚀📌|| 🔥✔️Beats 99.83%🎯 || 🎓🧠Easy to understand🔥|| Beginner friendly💀💯
CODES\njava []\nclass Solution {\n    public int candy(int[] ratings) {\n        int n = ratings.length;\n        int[] candies = new int[n];\n        for (int 
CODES\n```java []\nclass Solution {\n    public int candy(int[] ratings) {\n        int n = ratings.length;\n        int[] candies = new int[n];\n        for (int i=1; i<n; i++) {\n            if (ratings[i]>ratings[i-1]) {\n                candies[i]=candies[i-1]+1;\n            }\n        }\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            }\n        }\n        int ans=0;\n        for (int i: candies) {\n            ans+=i+1;\n        }\n        return ans;\n    }\n}\n```\n```Javascript []\nclass Solution {\n    candy(ratings) {\n        let n = ratings.length;\n        let candies = new Array(n).fill(1);\n        \n        for (let i = 1; i < n; i++) {\n            if (ratings[i] > ratings[i - 1]) {\n                candies[i] = candies[i - 1] + 1;\n            }\n        }\n        \n        for (let 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            }\n        }\n        \n        let ans = candies.reduce((sum, val) => sum + val, 0);\n        return ans;\n    }\n}\n\n```\n```Python []\nclass Solution:\n    def candy(self, ratings):\n        n = len(ratings)\n        candies = [1] * n\n        \n        for i in range(1, n):\n            if ratings[i] > ratings[i - 1]:\n                candies[i] = candies[i - 1] + 1\n        \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        \n        return sum(candies)\n```\n```C++ []\n#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n    int candy(std::vector<int>& ratings) {\n        int n = ratings.size();\n        std::vector<int> candies(n, 1);\n        \n        for (int i = 1; i < n; ++i) {\n            if (ratings[i] > ratings[i - 1]) {\n                candies[i] = candies[i - 1] + 1;\n            }\n        }\n        \n        for (int i = n - 2; i >= 0; --i) {\n            if (ratings[i] > ratings[i + 1]) {\n                candies[i] = std::max(candies[i], candies[i + 1] + 1);\n            }\n        }\n        \n        int ans = 0;\n        for (int candy : candies) {\n            ans += candy;\n        }\n        return ans;\n    }\n};\n```\n```C# []\nusing System;\n\npublic class Solution {\n    public int Candy(int[] ratings) {\n        int n = ratings.Length;\n        int[] candies = new int[n];\n        Array.Fill(candies, 1);\n        \n        for (int i = 1; i < n; i++) {\n            if (ratings[i] > ratings[i - 1]) {\n                candies[i] = candies[i - 1] + 1;\n            }\n        }\n        \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            }\n        }\n        \n        int ans = 0;\n        foreach (int candy in candies) {\n            ans += candy;\n        }\n        return ans;\n    }\n}\n\n```\n```C []\n#include <stdio.h>\n#include <stdlib.h>\n#include <stddef.h>\n\nint candy(int* ratings, int ratingsSize) {\n    int* candies = (int*)malloc(ratingsSize * sizeof(int));\n    for (int i = 0; i < ratingsSize; i++) {\n        candies[i] = 1;\n    }\n    \n    for (int i = 1; i < ratingsSize; i++) {\n        if (ratings[i] > ratings[i - 1]) {\n            candies[i] = candies[i - 1] + 1;\n        }\n    }\n    \n    for (int i = ratingsSize - 2; i >= 0; i--) {\n        if (ratings[i] > ratings[i + 1]) {\n            candies[i] = (candies[i] > candies[i + 1] + 1) ? candies[i] : candies[i + 1] + 1;\n        }\n    }\n    \n    int ans = 0;\n    for (int i = 0; i < ratingsSize; i++) {\n        ans += candies[i];\n    }\n    \n    free(candies);\n    return ans;\n}\n```\n\n![7be995b4-0cf4-4fa3-b48b-8086738ea4ba_1699897744.9062278.jpeg](https://assets.leetcode.com/users/images/fe5117c9-43b1-4ec8-8979-20c4c7f78d98_1721303757.4674635.jpeg)
['Array', 'Greedy', 'C', 'Python', 'C++', 'Java', 'JavaScript', 'C#']