My java solution


  • 1
    G
    public class Solution {
    public int candy(int[] ratings) {
        
        
        int result=0;
        int l=ratings.length;
        int[] values=new int[l];
        // In order
        for(int i=0;i<l;i++){
            if(i-1>=0&&ratings[i]>ratings[i-1]){
                values[i]=values[i-1]+1;
            }
            else{
                values[i]=1;
            }
        }
        // Reverse order
        for(int i=l-1;i>=0;i--){
            if(i+1<l&&ratings[i]>ratings[i+1]){
                if(values[i]<values[i+1]+1){
                    values[i]=values[i+1]+1;
                }
            }
            result+=values[i];
        }
        
        return result;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.