forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandy.java
More file actions
23 lines (23 loc) · 684 Bytes
/
Candy.java
File metadata and controls
23 lines (23 loc) · 684 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int candy(int[] ratings) {
int[] leftCandy = new int[ratings.length];
int[] rightCandy = new int[ratings.length];
Arrays.fill(leftCandy, 1);
Arrays.fill(rightCandy, 1);
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i - 1]) {
leftCandy[i] = leftCandy[i - 1] + 1;
}
}
for (int i = ratings.length - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
rightCandy[i] = rightCandy[i + 1] + 1;
}
}
int numOfCandies = 0;
for (int i = 0; i < ratings.length; i++) {
numOfCandies += Math.max(leftCandy[i], rightCandy[i]);
}
return numOfCandies;
}
}