-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandy.java
More file actions
231 lines (165 loc) · 6.34 KB
/
Candy.java
File metadata and controls
231 lines (165 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package Algorithms.GreedyAlgorithms;
import java.util.Arrays;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 22 June 2025
* @link 135. Candy <a href="https://leetcode.com/problems/candy/">Leetcode link</a>
* @topics Array, Greedy
* @description: You are giving candies to these children subjected to the following requirements:
* 1. Each child must have at least one candy.
* 2. Children with a higher rating get more candies than their neighbors.
*/
public class Candy {
public static void main(String[] args) {
int[] ratings = {1, 0, 2};
System.out.println(candy(ratings)); // Output: 5
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*
Here we have 3 cases:
1. Left rating is equal to right rating --- same [2, 2, 2]
2. Left rating is less than right rating --- increasing order [1, 2, 3]
3. Right rating is less than left rating --- decreasing order [3, 2, 1]
Example 1:
Given
ratings = [5, 4, 3, 5, 6, 4, 3, 2, 1]
initialize
candies = [1, 1, 1, 1, 1, 1, 1, 1, 1]
left to right pass
[5, 4, 3, 5, 6, 4, 3, 2, 1]
candies = [1, 1, 1, 2, 3, 1, 1, 1, 1]
right to left pass
[5, 4, 3, 5, 6, 4, 3, 2, 1]
candies = [3, 2, 1, 2, 4, 4, 3, 2, 1]
Example 2:
Given
ratings = [5, 4, 3, 5, 6, 7, 2, 1]
initialize
candies = [1, 1, 1, 1, 1, 1, 1, 1]
left to right pass
[5, 4, 3, 5, 6, 7, 2, 1]
candies = [1, 1, 1, 2, 3, 4, 1, 1]
right to left pass
[5, 4, 3, 5, 6, 7, 2, 1]
candies = [1, 1, 1, 2, 3, 4, 2, 1] ---> here at 7 rating, use Math.max(candies[i], candies[i+1]+1)
*/
public static int candy(int[] ratings) {
int n = ratings.length;
int[] candies = new int[n];
Arrays.fill(candies, 1);
// Left to right pass
for(int i=0; i<n; i++) {
int leftRating = i==0? ratings[i] : ratings[i-1];
int leftCandy = i==0? 1 : candies[i-1];
if (leftRating < ratings[i]) { // is left smaller -- increasing?
candies[i] = leftCandy+1;
}
}
// Right to left pass
for(int i=n-1; i>=0; i--) {
int rightRating = i==n-1? ratings[i] : ratings[i+1];
int rightCandy = i==n-1? 1 : candies[i+1];
if (ratings[i] > rightRating) { // is right smaller -- decreasing?
candies[i] = Math.max(candies[i], rightCandy + 1);;
}
}
// Sum up the candies
return Arrays.stream(candies).sum();
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*
* It is same as above {@link #candy(int[])} but we can skip i==0 and i==n-1
*/
public static int candy2(int[] ratings) {
int n = ratings.length;
int[] candies = new int[n];
Arrays.fill(candies, 1);
// Left to right pass
for (int i = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1]) {
candies[i] = candies[i - 1] + 1;
}
}
int sum = candies[n - 1];
// Right to left pass
for (int i = n - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
}
// we can also sum up the candies
sum += candies[i];
}
// Sum up the candies
return Arrays.stream(candies).sum(); // or return sum;
}
// using n^2 approach
public static int candy3(int[] ratings) {
int n = ratings.length;
int[] candies = new int[n];
Arrays.fill(candies, 1);
boolean changed = true;
while (changed) {
changed = false;
for (int i = 0; i < n; i++) {
if (i > 0 && ratings[i] > ratings[i - 1] && candies[i] <= candies[i - 1]) {
candies[i] = candies[i - 1] + 1;
changed = true;
}
if (i < n - 1 && ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) {
candies[i] = candies[i + 1] + 1;
changed = true;
}
}
}
return Arrays.stream(candies).sum();
}
/**
* NOT WORKING
1 [1, 0, 2] 2 --- ratings
[2, 1, 2] --- candies = 5
1 [1, 2, 2] 2 --- ratings
[1, 2, 1] --- candies = 4
1 [1, 0, 2, 9, 2, 1, 5, 6, 2] 2 --- ratings
[1, 1, 1, 1, 1, 1, 1, 1, 1] --- candies i=0
[2, 1, 1, 1, 1, 1, 1, 1, 1] --- candies i=1
[2, 1, 2, 1, 1, 1, 1, 1, 1] --- candies i=2
[2, 1, 2, 3, 1, 1, 1, 1, 1] --- candies i=3
[2, 1, 2, 3, 1, 1, 1, 1, 1] --- candies i=4
[2, 1, 2, 3, 2, 1, 2, 3, 1] --- candies = 17
1 [1, 2, 9, 9, 9, 2, 1] 1
[1, 1, 1, 1, 1, 1, 1] i=0
[1, 2, 1, 1, 1, 1, 1] i=1
[1, 2, 3, 1, 2, 2, 1] i=2
*/
public int candyMyApproachNotWorking(int[] ratings) {
int n = ratings.length;
int[] candies = new int[n];
Arrays.fill(candies, 1);
for(int i=0; i<n; i++) {
int curr = ratings[i];
int left = i==0? ratings[i] : ratings[i-1];
int right = i==n-1? ratings[i] : ratings[i+1];
if (curr <= left && curr <= right) continue;
int leftCandy = i==0? 1 : candies[i-1];
int rightCandy = i==n-1? 1 : candies[i+1];
// candies[i] = ((left<curr && curr>=right)? Math.max(leftCandy, rightCandy) : Math.min(leftCandy, rightCandy)) + 1;
candies[i] = ((left<curr)? leftCandy : rightCandy) + 1;
}
System.out.println(Arrays.toString(candies));
for(int i=n-1; i>=0; i--) {
int curr = ratings[i];
int left = i==0? ratings[i] : ratings[i-1];
int right = i==n-1? ratings[i] : ratings[i+1];
int currCandy = candies[i];
int leftCandy = i==0? 1 : candies[i-1];
int rightCandy = i==n-1? 1 : candies[i+1];
if(curr>right && currCandy<=rightCandy) candies[i] += rightCandy;
}
System.out.println(Arrays.toString(candies));
return Arrays.stream(candies).sum();
}
}