LeetCode - 621. Task Scheduler | Array | Python | C++

本文详细解析LeetCode的第621题——任务调度器,讨论如何在不违反CPU规定(即相同任务不能连续执行n个单位时间)的情况下,计算至少需要多少时间来处理所有任务。通过分析三种情况:单个最高频率任务、剩余任务数量超过空闲时间、多个最高频率任务,并给出相应的解决方案和通用公式。
摘要由CSDN通过智能技术生成

Question: Our CPU needs to take care of a list of tasks and some of the tasks they are same.  This perticular CPU doesn't allow two same tasks to be processed within n units.  The question is asking for how many units at least for this CPU to handle these tasks.  We know the total number of tasks the CPU needs to take care of so the key to this question is how many idles we need to add.

First, let's look at the most general case: we have a list of tasks and we have only one top freqency, and the rest number of tasks will not over the number of idles we have.

For example, A-3, B-2 and n=2
A _ _ A _ _ A We have 4 idles and we have two more tasks to handle.  They could just fill out the positions where the idles are.
So the number of idles is 2*2

If we generalize this, the number of idles = (the top frequency - 1)*n

The second case is that if the number of rest tasks is over the number of idles.

For example, A-3, B-2, C-2, D-2 and n=2
A _ _ A _ _ A We have 4 idles at the very beginning and we have 6 more tasks to handle.  If we fill like this, A B C A B C A.  Why do we put the D's? We could just put like this A B C D A B C D A and this will not violate the rule we set for this CPU.

If we generalize this, the number of extra units we add except the top frequency = max((the top frequency - 1)*n, the rest num of tasks)

The third case is that if we have multiple top frequencies.

For example, A-3, B-3, C-1 and n=2
This will only change how we calculate the numebr of idles if we look at A and B as a whole.
A B _ A B _ A B So we have two idles and one more task to handle.

If we generalize this, the number of ideles = (the top frquency - 1) * (n - the number of top frequencies)

So the final equation will be 
the number of extra units we add except the top fequencies  = max((the top frequency -1 )*(n - the number of top frequencies), the rest num of tasks)
This could also handle the case if the number of top frequencies is over n.

def leastInterval(self, tasks: List[str], n: int) -> int:
    maxFreq = 0
    countMax = 0
    status = {}
    for t in tasks:
        status[t] = status.get(t, 0) + 1
        if status[t]>maxFreq:
            countMax = 1
            maxFreq = status[t]
        elif status[t]==maxFreq:
            countMax += 1
    numOfIdles = (maxFreq-1)*(n-(countMax-1))
    res = maxFreq*countMax + max(numOfIdles, len(tasks)-maxFreq*countMax)
    return res

 

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        if(tasks.empty()) return 0;
        vector<int> count(26,0);
        for(char task:tasks){
            count[task-'A']++;
        }
        sort(count.begin(), count.end(), greater<>());
        int pre = count[0]-1;
        int res = (n+1)*pre;
        int temp = res;
        int i;
        for(i=0;count[i]==count[0];i++){
            res++;
            temp -=pre;
        }
        int cnt =0;
        for(;count[i]!=0;i++){
            temp -= count[i];
        }
        if(temp<0)
            res += -temp;
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值