✅ EASY | ✅ [ Python / Java / C++ / JavaScript / C# ] | 🔥100 % |
\nPython []\nclass Solution:\n    def candy(self, ratings: List[int]) -> int:\n        n = len(ratings)\n        if n <= 1:\n            return n\n\n        num
\n```Python []\nclass Solution:\n    def candy(self, ratings: List[int]) -> int:\n        n = len(ratings)\n        if n <= 1:\n            return n\n\n        nums = [1] * n\n\n        # Make sure children with a higher rating get more candy than their left neighbor\n        for i in range(1, n):\n            if ratings[i] > ratings[i - 1]:\n                nums[i] += nums[i - 1]\n\n    # Make sure children with a higher rating get more candy than their right neighbor\n        for i in range(n - 1, 0, -1):\n            if ratings[i - 1] > ratings[i]:\n                nums[i - 1] = max(nums[i] + 1, nums[i - 1])\n\n        candies = sum(nums)\n\n        return candies\n```\n```Java []\nint n = ratings.length;\n        if (n <= 1) return n;\n\n        int[] nums = new int[n];\n        Arrays.fill(nums, 1);\n\n        // Make sure children with a higher rating get more candy than their left neighbor\n        for (int i = 1; i < n; i++) {\n            if (ratings[i] > ratings[i - 1]) {\n                nums[i] += nums[i - 1];\n            }\n        }\n\n        // Make sure children with a higher rating get more candy than their right neighbor\n        for (int i = n - 1; i > 0; i--) {\n            if (ratings[i - 1] > ratings[i]) {\n                nums[i - 1] = Math.max(nums[i] + 1, nums[i - 1]);\n            }\n        }\n\n        int candies = 0;\n        for (int e : nums) {\n            candies += e;\n        }\n\n        return candies;\n```\n```C++ []\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int n = ratings.size();\n        if(n <= 1) return n;\n\n        vector<int> nums( n , 1);\n        // make sure children with a higher rating gets more candy than its left neighbour\n        for(int i =1 ; i< n ; i++){\n            if(ratings[i] > ratings[i-1]){\n                nums[i]+=nums[i-1];\n            }\n        }\n        // make sure children with higher rating get more candy than its right neighbour\n        for(int i = n-1 ; i>0 ; i--){\n            if(ratings[i-1] > ratings[i]){\n                nums[i-1] = max(nums[i]+1 , nums[i-1]);\n            }\n        }\n\n        int candies = 0;\n\n        for(auto e : nums) candies+=e;\n\n        return candies;\n    }\n};\n```\n```JavaScript []\nconst n = ratings.length;\n    if (n <= 1) return n;\n\n    const nums = new Array(n).fill(1);\n\n    // Make sure children with a higher rating get more candy than their left neighbor\n    for (let i = 1; i < n; i++) {\n        if (ratings[i] > ratings[i - 1]) {\n            nums[i] += nums[i - 1];\n        }\n    }\n\n    // Make sure children with higher rating get more candy than their right neighbor\n    for (let i = n - 1; i > 0; i--) {\n        if (ratings[i - 1] > ratings[i]) {\n            nums[i - 1] = Math.max(nums[i] + 1, nums[i - 1]);\n        }\n    }\n\n    let candies = 0;\n\n    for (const e of nums) {\n        candies += e;\n    }\n\n    return candies;\n```\n```C# []\n int n = ratings.Length;\n    if (n <= 1) return n;\n\n    int[] nums = new int[n];\n    for (int i = 0; i < n; i++)\n    {\n        nums[i] = 1;\n    }\n\n    // Make sure children with a higher rating get more candy than their left neighbor\n    for (int i = 1; i < n; i++)\n    {\n        if (ratings[i] > ratings[i - 1])\n        {\n            nums[i] += nums[i - 1];\n        }\n    }\n\n    // Make sure children with higher rating get more candy than their right neighbor\n    for (int i = n - 1; i > 0; i--)\n    {\n        if (ratings[i - 1] > ratings[i])\n        {\n            nums[i - 1] = Math.Max(nums[i] + 1, nums[i - 1]);\n        }\n    }\n\n    int candies = 0;\n    foreach (int e in nums)\n    {\n        candies += e;\n    }\n\n    return candies;\n```\n
['C++', 'Java', 'Python3', 'JavaScript', 'C#']