Skip to content

Commit 09dee4b

Browse files
committed
Runtime: 16 ms (Top 14.09%) | Memory: 53.1 MB (Top 8.48%)
1 parent c8c9bb5 commit 09dee4b

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

scripts/algorithms/C/Candy/Candy.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
// Runtime: 16 ms (Top 14.09%) | Memory: 53.1 MB (Top 8.48%)
12
class Solution {
23
public int candy(int[] ratings) {
3-
4+
45
int[] left = new int[ratings.length];
56
Arrays.fill(left, 1);
6-
7+
78
// we are checking from left to right that if the element next to our current element has greater rating, if yes then we are increasing their candy
89
for(int i = 0; i<ratings.length-1; i++){
910
if(ratings[i] < ratings[i+1])
1011
left[i+1] = left[i]+1;
1112
}
12-
13+
1314
int[] right = new int[ratings.length];
1415
Arrays.fill(right, 1);
15-
16+
1617
//we are checking from right to left if the element after than our current element is greater or not , if yes then we are also checking their candies if greater rating has less number of candies then increasing their candy
1718
for(int i = ratings.length -2; i>=0; i--){
1819
if(ratings[i+1] < ratings[i] && right[i] <= right[i+1])
@@ -22,6 +23,6 @@ public int candy(int[] ratings) {
2223
for(int i = 0; i<right.length; i++){
2324
sum += Math.max(right[i], left[i]);
2425
}
25-
26+
2627
return sum;}
27-
}
28+
}

0 commit comments

Comments
 (0)