Moye Moye

 avatar
bruteCoder
java
2 months ago
623 B
3
Indexable
Never
class Solution {
    static int minCandy(int N, int rat[]) {
        // code here
        int[] left = new int[N];
        int[] right = new int[N];
        Arrays.fill(left,1);
        Arrays.fill(right,1);
        
        for(int i = 1; i<N;i++)
        {
            if(rat[i] > rat[i-1]) left[i] = left[i-1]+1;
        }
        
        for(int i = N-2;i>=0;i--){
            if(rat[i] > rat[i+1]) right[i]= right[i+1]+1;
        }
        
        int out = 0 ;
        
        for(int i = 0 ;i<N;i++)
        {
            out += Math.max(left[i],right[i]);
        }
        
        return out;
        
    }
}
Leave a Comment