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
The newest version!
package g0101_0200.s0135_candy;
// #Hard #Array #Greedy #2022_06_24_Time_2_ms_(99.95%)_Space_42.8_MB_(94.28%)
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 candy : candies) {
total += candy;
}
return total;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy