621. Task Scheduler

621. Task Scheduler

标签(空格分隔): leetcode array medium


题目

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.

Example 1:

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

Note:

  1. The number of tasks is in the range [1, 10000].
  2. The integer n is in the range [0, 100].

思路

本题是一个CPU资源调度题,给定一个字符数组,里面的字符是大写字母"A"-“Z”,一个字符代表一个CPU任务,执行一个任务需要一个始终周期,执行不同的任务可以直接执行,执行相同的任务需要间隔n个时钟周期,这里的n,题目会给出。
学过体系结构的童鞋,做这个题目就得心应手,我们首先找到最大频繁项的任务TASK,然后我们可以通过将其分隔成M块(M表示TASK的个数)。然后再TASK之间插入其他不同的任务,我们直接来举例说明。

AAAABBBEEFFGG 3

Frame: "AXXXAXXXAXXXA"
insert 'B': "ABXXABXXABXXA" <--- 'B' has higher frequency than the other characters, insert it first.
insert 'E': "ABEXABEXABXXA"
insert 'F': "ABEFABEXABFXA" <--- each time try to fill the k-1 gaps as full or evenly as possible.
insert 'G': "ABEFABEGABFGA"

AACCCBEEE 2

3 identical chunks "CE", "CE CE CE" <-- this is a frame
insert 'A' among the gaps of chunks since it has higher frequency than 'B' ---> "CEACEACE"
insert 'B' ---> "CEABCEACE" <----- result is tasks.length;

AACCCDDEEE 3

3 identical chunks "CE", "CE CE CE" <--- this is a frame.
Begin to insert 'A'->"CEA CEA CE"
Begin to insert 'B'->"CEABCEABCE" <---- result is tasks.length;

ACCCEEE 2

3 identical chunks "CE", "CE CE CE" <-- this is a frame
Begin to insert 'A' --> "CEACE CE" <-- result is (c[25] - 1) * (n + 1) + 25 -i = 2 * 3 + 2 = 8

最终我们总结出了一个公式:(c[25] - 1) * (n + 1) + 25 -i


代码

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        vector<int> res(26);
        for(auto task:tasks){
            res[task-'A']++;
        }
        sort(res.begin(),res.end());
        int i=25;
        while(i>=0&res[i]==res[25]){
            i--;
        }   
        return tasks.size()>(res[25]-1)*(n+1)+25-i?tasks.size():(res[25]-1)*(n+1)+25-i;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Roaring Kitty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值