Share my c solution XD


  • 0
    E
    int candy(int* ratings, int ratingsSize) {
        int ret=0;
        if(ratingsSize==0) return 0;
        if(ratingsSize==1) return 1;
        int count=0;
        int *candies=malloc(sizeof(int)*(ratingsSize+1));
        memset(candies,0,sizeof(int)*(ratingsSize+1));
        int i;
        for(i=1;i<ratingsSize;i++){
            if(ratings[i]>=ratings[i-1]){
                if(ratings[i]>ratings[i-1]) candies[i]=candies[i-1]+1;
            }
        }
        for(i=ratingsSize-2;i>=0;i--){
            if(ratings[i]>=ratings[i+1]){
               if(ratings[i]>ratings[i+1]&&candies[i]<candies[i+1]+1) candies[i]=candies[i+1]+1;
            }
        }
        for(i=0;i<ratingsSize;i++){
            ret=ret+candies[i];
        }
        return ret+ratingsSize;
    }

Log in to reply
 

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