Repository navigation
Expand file tree
/
Copy pathsolution.java
More file actions
69 lines (62 loc) · 2.74 KB
/
Copy pathsolution.java
File metadata and controls
69 lines (62 loc) · 2.74 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
// 135. Candy
// https://leetcode.com/problems/candy/
// Hard | Java | Accepted 2026-09-06
// Runtime 82 ms | Memory 54.8 MB
class Solution {
public int candy(int[] ratings) {
//My original intuition was to use a priority queue that sorted the children by their ratings while also attaching their position in the array
//Runtime: O(NlogN)
PriorityQueue<int[]> ratin = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
for(int i = 0; i<ratings.length; i++){
ratin.add(new int[]{ratings[i], i}); //Sort the children from lowest to highest rating
}
int count = 0;
int[] candies = new int[ratings.length]; //Keep a candies array to keep track
while(!ratin.isEmpty())
{
int[] kid = ratin.poll(); //Poll the current kid
int rating = kid[0]; //Extract their rating and their index
int index = kid[1];
int candy = 0; //The amount of candies to give to the current kid
if(index+1<ratings.length && ratings[index+1]<rating) //If their rating is larger than the kid to their right, then set the amount of candies to the amount of candies the kid to their right got
{
candy = candies[index+1];
}
if(index-1>=0 && ratings[index-1]<rating) //If their rating is larger than the kid to their left, then set the amount of candies to whatever's larger, the candy amount or the amount of candies the kid to their left got
{
candy = Math.max(candy, candies[index-1]);
}
candies[index] = candy+1; //Add 1 since we need to have more candies than the amount of candies associated with our neighbors
count+=candy+1; //Add that to the global candy count
}
return count;
}
}
//There's an O(N) solution that requires a two pass greedy approach:
/*
class Solution {
public int candy(int[] ratings) {
int n = ratings.length;
int[] candies = new int[n];
// Step 1: Initialize all children with 1 candy
for (int i = 0; i < n; i++) {
candies[i] = 1;
}
// Step 2: Left-to-right pass
for (int i = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1]) {
candies[i] = candies[i - 1] + 1;
}
}
// Step 3: Right-to-left pass and calculate total
int totalCandies = candies[n - 1]; // add the last one beforehand
for (int i = n - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
}
totalCandies += candies[i];
}
return totalCandies;
}
}
*/