All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0135_candy.Solution Maven / Gradle / Ivy

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