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

g0101_0200.s0135_candy.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy