Skip to content

Commit 45229bd

Browse files
Candy
1 parent 36a9dbf commit 45229bd

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ My accepted leetcode solutions to some of the common interview problems.
210210
- [The Skyline Problem](problems/src/heap/TheSkylineProblem.java) (Hard)
211211
- [Meeting Rooms II](problems/src/heap/MeetingRoomsII.java) (Medium)
212212
- [Top K Frequent Words](problems/src/heap/TopKFrequentWords.java) (Medium)
213+
- [Candy](problems/src/heap/Candy.java) (Hard)
213214

214215
#### [Linked List](problems/src/linked_list)
215216

problems/src/heap/Candy.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package heap;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 26/07/2018.
7+
* There are N children standing in a line. Each child is assigned a rating value.
8+
9+
You are giving candies to these children subjected to the following requirements:
10+
11+
Each child must have at least one candy.
12+
Children with a higher rating get more candies than their neighbors.
13+
What is the minimum candies you must give?
14+
15+
Example 1:
16+
17+
Input: [1,0,2]
18+
Output: 5
19+
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
20+
Example 2:
21+
22+
Input: [1,2,2]
23+
Output: 4
24+
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
25+
The third child gets 1 candy because it satisfies the above two conditions.
26+
27+
Solution: O(N log N): Store the indexes in a heap, iterate through the heap one by one and assign candies one
28+
greater than its neighbours. Take care of edge cases.
29+
30+
*/
31+
public class Candy {
32+
33+
/**
34+
* Main method
35+
* @param args
36+
* @throws Exception
37+
*/
38+
public static void main(String[] args) throws Exception{
39+
int[] ratings = {29,51,87,87,72,12};
40+
System.out.println(new Candy().candy(ratings));
41+
}
42+
43+
public int candy(int[] ratings) {
44+
if(ratings.length == 1) return 1;
45+
PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(ratings[o1], ratings[o2]));
46+
for(int i = 0; i < ratings.length; i ++){
47+
pq.offer(i);
48+
}
49+
int[] count = new int[ratings.length];
50+
while (!pq.isEmpty()){
51+
int index = pq.poll();
52+
if(index - 1 < 0){
53+
if(ratings[index + 1] == ratings[index]){
54+
count[index] = 1;
55+
} else{
56+
count[index] = count[index + 1] + 1;
57+
}
58+
} else if(index + 1 >= ratings.length) {
59+
if(ratings[index - 1] == ratings[index]){
60+
count[index] = 1;
61+
} else{
62+
count[index] = count[index - 1] + 1;
63+
}
64+
} else{
65+
if((ratings[index - 1] == ratings[index]) && (ratings[index + 1] == ratings[index])){
66+
count[index] = 1;
67+
} else{
68+
if(((ratings[index - 1] == ratings[index]) && (ratings[index + 1] > ratings[index]))
69+
|| ((ratings[index + 1] == ratings[index]) && (ratings[index - 1] > ratings[index]))){
70+
count[index] = 1;
71+
} else if(((ratings[index - 1] == ratings[index]) && (ratings[index + 1] < ratings[index]))){
72+
count[index] = count[index + 1] + 1;
73+
} else if(((ratings[index + 1] == ratings[index]) && (ratings[index - 1] < ratings[index]))){
74+
count[index] = count[index - 1] + 1;
75+
}
76+
else {
77+
if(count[index - 1] > count[index + 1]){
78+
count[index] = count[index - 1] + 1;
79+
} else {
80+
count[index] = count[index + 1] + 1;
81+
}
82+
}
83+
}
84+
}
85+
}
86+
int result = 0;
87+
for(int c : count){
88+
result += c;
89+
}
90+
return result;
91+
}
92+
}

0 commit comments

Comments
 (0)