g0101_0200.s0135_candy.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0101_0200.s0135_candy;
// #Hard #Array #Greedy #2022_02_22_Time_3_ms_(76.49%)_Space_51.4_MB_(28.53%)
import java.util.Arrays;
public class Solution {
public int candy(int[] ratings) {
int[] candies = new int[ratings.length];
Arrays.fill(candies, 1);
for (int i = 0; i < ratings.length - 1; i++) {
if (ratings[i + 1] > ratings[i]) {
candies[i + 1] = candies[i] + 1;
}
}
for (int i = ratings.length - 1; i > 0; i--) {
if (ratings[i - 1] > ratings[i] && candies[i - 1] < candies[i] + 1) {
candies[i - 1] = candies[i] + 1;
}
}
int total = 0;
for (int i = 0; i < candies.length; i++) {
total += candies[i];
}
return total;
}
}