classSolution{public:intleastInterval(vector<char>&tasks,intn){if(n==0)returntasks.size();vector<int>count(26);for(constchartask:tasks)++count[task-'A'];constintmaxFreq=*max_element(begin(count),end(count));// Put the most frequent task in the slot firstconstintmaxFreqTaskOccupy=(maxFreq-1)*(n+1);// Get # of tasks with same frequency as maxFreq,// we'll append them after maxFreqTaskOccupyconstintnMaxFreq=std::count(begin(count),end(count),maxFreq);// Max(// the most frequent task is frequent enough to force some idle slots,// the most frequent task is not frequent enough to force idle slots// )returnmax(maxFreqTaskOccupy+nMaxFreq,static_cast<int>(tasks.size()));}};
classSolution{publicintleastInterval(char[]tasks,intn){int[]count=newint[26];for(finalchartask:tasks)++count[task-'A'];finalintmaxFreq=Arrays.stream(count).max().getAsInt();// Put the most frequent task in the slot firstfinalintmaxFreqTaskOccupy=(maxFreq-1)*(n+1);// Get # of tasks with same frequency as maxFreq,// we'll append them after maxFreqTaskOccupyfinalintnMaxFreq=(int)Arrays.stream(count).filter(c->c==maxFreq).count();// Max(// the most frequent task is frequent enough to force some idle slots,// the most frequent task is not frequent enough to force idle slots// )returnMath.max(maxFreqTaskOccupy+nMaxFreq,tasks.length);}}
classSolution:defleastInterval(self,tasks:List[str],n:int)->int:count=Counter(tasks)maxFreq=max(count.values())# Put the most frequent task in the slot firstmaxFreqTaskOccupy=(maxFreq-1)*(n+1)# Get # Of tasks with same frequency as maxFreq,# we'll append them after maxFreqTaskOccupynMaxFreq=sum(value==maxFreqforvalueincount.values())# Max(# the most frequent task is frequent enough to force some idle slots,# the most frequent task is not frequent enough to force idle slots# )returnmax(maxFreqTaskOccupy+nMaxFreq,len(tasks))