public int candy2(int[] ratings) {
        if(ratings == null || ratings.length == 0) return 0;
        if(ratings.length == 1) return 1;

        int L = ratings.length;
        int [] candies = new int [L];
        Arrays.fill(candies, 1);

        for(int i = 1; i < L; ++i){
            if(ratings[i] > ratings[i-1]){
                candies[i] = candies[i-1] + 1;
            }
        }

        for(int i = L - 1; i >= 1; --i){
            if(ratings[i-1] > ratings[i] && candies[i-1] <= candies[i]){
                candies[i-1] = candies[i] + 1;
            }
        }

        int res = 0;
        for(int i = 0; i < L; ++i) res += candies[i];
        return res;
    }