JAVA || EFFICIENT || EASIEST CODE WITH EXPLANATION ✅
Hint (First Try Yourself)\nThe key observation in this problem is that the greedy approach works by ensuring two conditions:  \n1. When going from left to right
# Hint (First Try Yourself)\nThe key observation in this problem is that the greedy approach works by ensuring two conditions:  \n1. When going from left to right, if a child\'s rating is higher than the previous child\'s, they must get more candies.\n2. When going from right to left, if a child\'s rating is higher than the next child\'s and they have equal or fewer candies than the next child, they must also get more candies.\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve this problem, we need to ensure that every child gets at least one candy, and that children with higher ratings receive more candies than their neighbors. The greedy approach allows us to achieve this by using two passes over the array.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize a `candies` array to track the minimum number of candies each child should get, starting with 1 candy for everyone.\n2. Perform a **left-to-right pass** over the `ratings` array: \n   - If a child\'s rating is higher than the previous child\u2019s, increase their candy count by one.\n3. Perform a **right-to-left pass**:\n   - If a child\'s rating is higher than the next child\u2019s and they have fewer or the same candies, increase their candy count by one.\n4. Finally, sum up the values in the `candies` array to calculate the total number of candies needed.\n\n# Complexity\n- **Time complexity**: \n  $$O(n)$$, where `n` is the number of children. We loop through the `ratings` array twice (left-to-right and right-to-left).\n  \n- **Space complexity**: \n  $$O(n)$$, where `n` is the number of children, because we need to store the `candies` array.\n\n# Example\n<!-- Provide a visual or step-by-step iterative example of the solution. -->\nConsider the input `ratings = [1, 0, 2]`.\n\n1. **Initialization**:\n   - `candies = [1, 1, 1]` (every child gets at least 1 candy initially).\n\n2. **Left-to-right pass**:\n   - For `i = 1`: `ratings[1] < ratings[0]` (no change).\n   - For `i = 2`: `ratings[2] > ratings[1]`, so `candies[2] = candies[1] + 1 = 2`.\n   - Now `candies = [1, 1, 2]`.\n\n3. **Right-to-left pass**:\n   - For `i = 1`: `ratings[1] < ratings[2]` (no change).\n   - For `i = 0`: `ratings[0] > ratings[1]`, so `candies[0] = candies[1] + 1 = 2`.\n   - Now `candies = [2, 1, 2]`.\n\n4. **Summing up candies**: \n   - Total candies = `2 + 1 + 2 = 5`.\n\nThus, the minimum number of candies needed is `5`.\n\n# Code\n```java\n\nclass Solution {\n    public int candy(int[] ratings) {\n        int n = ratings.length;\n        int count = 0;\n        int[] arr = new int[n];\n        Arrays.fill(arr, 1);\n\n        for (int i = 1; i < n; i++) {\n            if (ratings[i] > ratings[i - 1]) {\n                arr[i] = arr[i - 1] + 1; \n            }\n        }\n\n        for (int i = n - 2; i >= 0; i--) {\n            if (ratings[i] > ratings[i + 1] && arr[i] <= arr[i + 1]) {\n                arr[i] = arr[i + 1] + 1;\n            }\n        }\n\n        for (int i : arr) count += i;\n\n        return count;\n    }\n}\n```\n\nPlease upvote if you liked the answer :)\n
['Java']