Why runtime error in Candy ? gives correct output locally! last run [1,1,1]


  • 0
    E

    class Solution {
    public:
    int candy(vector<int> &ratings) {

        int numberofChildren= ratings.size();
        vector<int> candies(numberofChildren);
        std::fill(candies.begin(),candies.end(),1);
    
        int i=0,j=numberofChildren;
        while(i<=j)
        {
            //cout << i ;
            if((ratings[i] > ratings[i+1]) && (candies[i]<=candies[i+1]))
            {
                candies[i]=candies[i+1]+1;
            }
             else if((ratings[i+1] > ratings[i]) && (candies[i]>=candies[i+1]))
            {
                candies[i+1]=candies[i]+1;
            }
            i++;
    
        }
    
        i=0,j=numberofChildren;
        while(i<=j)
        {
         if((ratings[j] > ratings[j-1]) && (candies[j]<=candies[j-1]))
         {
    
             candies[j]=candies[j-1]+1;
             }
             else if((ratings[j-1] > ratings[j]) && (candies[j]>=candies[j-1]))
         {
    
             candies[j-1]=candies[j]+1;
             }
             j--;
         }
    
        int sum=0;
        for(int k=0;k < candies.size();k++)
        {
        sum=sum+candies[k];
        }
    

    // cout << "The sum of the candies are " << sum << endl;
    return sum;
    }
    };


Log in to reply
 

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