首页 > 代码库 > leetcode分配糖果问题

leetcode分配糖果问题

 

    int candy(vector<int> &ratings) {        vector<int> priority(ratings);        for(int i=0;i<priority.size();i++)  ratings[i]=1;        //_从右往左比        for(int i=1;i<priority.size();i++)	    {		    if(priority[i]>priority[i-1]&&ratings[i]<=ratings[i-1])	    ratings[i]=ratings[i-1]+1;		    //if(priority[i]==priority[i-1])						        ratings[i]=ratings[i-1];	//_晕,不需考虑同级情况,要注释掉才对	    }	    //_从左往右比	    for(int i=priority.size()-1;i>0;i--)	    {		    if(priority[i-1]>priority[i]&&ratings[i-1]<=ratings[i])	    ratings[i-1]=ratings[i]+1;	    	//if(priority[i-1]==priority[i])						        ratings[i-1]=ratings[i];	    }	    int sum=0;	    for(int i=0;i<priority.size();i++)  sum+=ratings[i];	    return sum;    }

 

leetcode分配糖果问题