内容:
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.
You need to return the least number of intervals the CPU will take to finish all the given tasks.
思路:模拟CPU任务分配,A 到 Z表示不同的任务,任务可以以不同顺序进行。每个任务可以在一个时间间隔中完成。对于一个时间间隔,CPU可以执行一个任务或者是闲置。但是,两个同样
public int leastInterval(char[] tasks, int n) {
int[] freq=new int[26];
int maxFreq=0,maxFreqCount=0;
for(int i=0;i<tasks.length;i++){
freq[tasks[i]-'A']++;
}
for(int i=0;i<26;i++){
if(freq[i]>maxFreq){
maxFreq=freq[i];
maxFreqCount=1;
}else if(freq[i]==maxFreq){
maxFreqCount++;
}
}
return Math.max(tasks.length,(maxFreq-1)*(n+1)+maxFreqCount);
}的任务之间需要有 n 个冷却时间,也就是说假如A执行后,那么未来n个时间间隔内,A是不允许再执行的。
本文介绍了一种CPU任务调度算法,该算法考虑了不同任务间的冷却时间要求,通过模拟任务分配来确保任务能在最短的时间内完成。算法核心在于计算最大频率任务的数量及频率,进而得出最小所需时间。
369

被折叠的 条评论
为什么被折叠?



