EASY ONE || GREEDY || JAVA C++ PYTHON
\n\n1. Initialization:\n   - Two arrays left and right are created to store the number of candies for each child from the left and right directions, respectivel
\n\n1. **Initialization:**\n   - Two arrays `left` and `right` are created to store the number of candies for each child from the left and right directions, respectively.\n   - Both arrays are initialized with 1 for the first and last elements, as they always need at least one candy.\n\n2. **Calculating Candies from the Left:**\n   - The code iterates through the `nums` array from the second element to the end.\n   - If the current rating is higher than the previous rating, the current child receives one more candy than the previous child.\n   - Otherwise, the current child receives only one candy.\n\n3. **Calculating Candies from the Right:**\n   - The code iterates through the `nums` array in reverse order from the second-to-last element to the beginning.\n   - Similar logic is applied to calculate the number of candies from the right side based on the ratings.\n\n4. **Determining Maximum Candies:**\n   - The code iterates through the `nums` array and calculates the maximum of `left[i]` and `right[i]` for each child. This ensures that each child receives the maximum number of candies based on both left and right comparisons.\n   - The maximum candies for all children are summed and stored in `ans`.\n\n5. **Return Maximum Candies:**\n\n\n\n# Code\n```java []\nclass Solution {\n    public int candy(int[] nums) {\n        int [] left=new int[nums.length];\n        left[0]=1;\n        int [] right=new int[nums.length];\n        right[nums.length-1]=1;\n        int ans=0;\n        for(int i=1;i<nums.length;i++){\n            if(nums[i]>nums[i-1]){\n                left[i]=left[i-1]+1;\n            }\n            \n            else{\n                left[i]=1;\n            }\n        }\n        for(int i=nums.length-2;i>=0;i--){\n            if(nums[i]>nums[i+1]){\n                right[i]=right[i+1]+1;\n            }\n            \n            else{\n                right[i]=1;\n            }\n        }\n        for(int i=0;i<nums.length;i++){\n            ans+=Math.max(left[i],right[i]);\n        }\n        return ans;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int n = ratings.size();\n        vector<int> left(n, 1);\n        vector<int> right(n, 1);\n        int ans = 0;\n\n        // Fill the left array\n        for (int i = 1; i < n; ++i) {\n            if (ratings[i] > ratings[i - 1]) {\n                left[i] = left[i - 1] + 1;\n            }\n        }\n\n        // Fill the right array\n        for (int i = n - 2; i >= 0; --i) {\n            if (ratings[i] > ratings[i + 1]) {\n                right[i] = right[i + 1] + 1;\n            }\n        }\n\n        // Calculate the total candies needed\n        for (int i = 0; i < n; ++i) {\n            ans += max(left[i], right[i]);\n        }\n\n        return ans;\n    }\n};\n```\n```python []\nclass Solution:\n    def candy(self, ratings: List[int]) -> int:\n        n = len(ratings)\n        left = [1] * n\n        right = [1] * n\n\n        # Fill the left array\n        for i in range(1, n):\n            if ratings[i] > ratings[i - 1]:\n                left[i] = left[i - 1] + 1\n\n        # Fill the right array\n        for i in range(n - 2, -1, -1):\n            if ratings[i] > ratings[i + 1]:\n                right[i] = right[i + 1] + 1\n\n        # Calculate the total candies needed\n        ans = sum(max(left[i], right[i]) for i in range(n))\n\n        return ans\n```\n\n
['Greedy', 'Python', 'C++', 'Java']