|
| 1 | +--- |
| 2 | +id: candy |
| 3 | +title: Candy |
| 4 | +sidebar_label: 0135 Candy |
| 5 | +tags: |
| 6 | + - Greedy |
| 7 | + - LeetCode |
| 8 | + - Java |
| 9 | + - Python |
| 10 | + - C++ |
| 11 | +description: "This is a solution to the Candy problem on LeetCode." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings. |
| 17 | + |
| 18 | +You are giving candies to these children subjected to the following requirements: |
| 19 | + |
| 20 | +- Each child must have at least one candy. |
| 21 | +- Children with a higher rating get more candies than their neighbors. |
| 22 | + |
| 23 | +Return the minimum number of candies you need to have to distribute the candies to the children. |
| 24 | + |
| 25 | +### Examples |
| 26 | + |
| 27 | +**Example 1:** |
| 28 | + |
| 29 | +``` |
| 30 | +Input: ratings = [1,0,2] |
| 31 | +Output: 5 |
| 32 | +Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively. |
| 33 | +
|
| 34 | +``` |
| 35 | + |
| 36 | +**Example 2:** |
| 37 | + |
| 38 | +Input: ratings = [1,2,2] |
| 39 | +Output: 4 |
| 40 | +Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively. |
| 41 | +The third child gets 1 candy because it satisfies the above two conditions. |
| 42 | + |
| 43 | +``` |
| 44 | +Input: root = [1,2,2,null,3,null,3] |
| 45 | +Output: false |
| 46 | +``` |
| 47 | + |
| 48 | +### Constraints |
| 49 | + |
| 50 | +- $n == ratings.length$ |
| 51 | +- $1 <= n <= 2 * 10^4$ |
| 52 | +- $0 <= ratings[i] <= 2 * 10^4$ |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Solution for Candy Distribution Problem |
| 57 | + |
| 58 | +### Intuition And Approach |
| 59 | + |
| 60 | +To solve this problem, we can use a greedy approach. We can start by assigning each child 1 candy. Then, we iterate over the ratings array twice, once from left to right and once from right to left, adjusting the number of candies as needed to satisfy the given conditions. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +#### Code in Different Languages |
| 65 | + |
| 66 | +<Tabs> |
| 67 | + <TabItem value="Java" label="Java"> |
| 68 | + <SolutionAuthor name="@YourName"/> |
| 69 | + ```java |
| 70 | +public class Solution { |
| 71 | + public int candy(int[] ratings) { |
| 72 | + int n = ratings.length; |
| 73 | + int[] candies = new int[n]; |
| 74 | + |
| 75 | + // Initialize all candies to 1 |
| 76 | + for (int i = 0; i < n; i++) { |
| 77 | + candies[i] = 1; |
| 78 | + } |
| 79 | + |
| 80 | + // Left to right pass |
| 81 | + for (int i = 1; i < n; i++) { |
| 82 | + if (ratings[i] > ratings[i - 1]) { |
| 83 | + candies[i] = candies[i - 1] + 1; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Right to left pass |
| 88 | + for (int i = n - 2; i >= 0; i--) { |
| 89 | + if (ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) { |
| 90 | + candies[i] = candies[i + 1] + 1; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + int totalCandies = 0; |
| 95 | + for (int candy : candies) { |
| 96 | + totalCandies += candy; |
| 97 | + } |
| 98 | + |
| 99 | + return totalCandies; |
| 100 | + } |
| 101 | + |
| 102 | + public static void main(String[] args) { |
| 103 | + Solution solution = new Solution(); |
| 104 | + int[] ratings1 = {1, 0, 2}; |
| 105 | + int[] ratings2 = {1, 2, 2}; |
| 106 | + |
| 107 | + System.out.println(solution.candy(ratings1)); // Output: 5 |
| 108 | + System.out.println(solution.candy(ratings2)); // Output: 4 |
| 109 | + } |
| 110 | +} |
| 111 | + ``` |
| 112 | + </TabItem> |
| 113 | + <TabItem value="Python" label="Python"> |
| 114 | + <SolutionAuthor name="@YourName"/> |
| 115 | + ```python |
| 116 | +class Solution(object): |
| 117 | + def candy(self, ratings): |
| 118 | + """ |
| 119 | + :type ratings: List[int] |
| 120 | + :rtype: int |
| 121 | + """ |
| 122 | + n = len(ratings) |
| 123 | + candies = [1] * n |
| 124 | + |
| 125 | + # Left to right pass |
| 126 | + for i in range(1, n): |
| 127 | + if ratings[i] > ratings[i - 1]: |
| 128 | + candies[i] = candies[i - 1] + 1 |
| 129 | + |
| 130 | + # Right to left pass |
| 131 | + for i in range(n - 2, -1, -1): |
| 132 | + if ratings[i] > ratings[i + 1]: |
| 133 | + candies[i] = max(candies[i], candies[i + 1] + 1) |
| 134 | + |
| 135 | + return sum(candies) |
| 136 | + ``` |
| 137 | + </TabItem> |
| 138 | + <TabItem value="C++" label="C++"> |
| 139 | + <SolutionAuthor name="@mahek0620"/> |
| 140 | + ```cpp |
| 141 | +#include <vector> |
| 142 | +#include <algorithm> |
| 143 | + |
| 144 | +using namespace std; |
| 145 | + |
| 146 | +class Solution { |
| 147 | +public: |
| 148 | + int candy(vector<int>& ratings) { |
| 149 | + int n = ratings.size(); |
| 150 | + vector<int> candies(n, 1); |
| 151 | + |
| 152 | + // Left to right pass |
| 153 | + for (int i = 1; i < n; i++) { |
| 154 | + if (ratings[i] > ratings[i - 1]) { |
| 155 | + candies[i] = candies[i - 1] + 1; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Right to left pass |
| 160 | + for (int i = n - 2; i >= 0; i--) { |
| 161 | + if (ratings[i] > ratings[i + 1]) { |
| 162 | + candies[i] = max(candies[i], candies[i + 1] + 1); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Sum up all candies |
| 167 | + int totalCandies = 0; |
| 168 | + for (int candy : candies) { |
| 169 | + totalCandies += candy; |
| 170 | + } |
| 171 | + |
| 172 | + return totalCandies; |
| 173 | + } |
| 174 | +}; |
| 175 | + ``` |
| 176 | + </TabItem> |
| 177 | +</Tabs> |
| 178 | + |
| 179 | +#### Complexity Analysis |
| 180 | + |
| 181 | +- Time Complexity: $O(n)$ where n is the number of children (length of the ratings array). |
| 182 | +- Space Complexity: $O(n)$ for storing the candies array. |
| 183 | + |
| 184 | +## References |
| 185 | + |
| 186 | +- **LeetCode Problem:** [Candy Problem](https://leetcode.com/problems/candy/) |
| 187 | +- **Solution Link:** [Candy Solution on LeetCode](https://leetcode.com/problems/candy/solutions/5273312/candy-solution) |
| 188 | +- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
0 commit comments