621. Task Scheduler

来源:互联网 发布:聚合数据 奖项 编辑:程序博客网 时间:2024/06/15 02:58

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 = 2Output: 8Explanation: 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 frameinsert '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 frameBegin 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;    }};
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 黄金计划软件 上全狐网 黄金路国语 黄金路中文字幕 黄金渔场 fx 黄金渔场121010 黄金渔场130606 黄金渔场20120905 黄金渔场20120912 黄金渔场fx 黄金渔场成龙 黄金渔场李弘基 黄敬家族关系图 黄敬简历 黄静茵 金勇俊 黄静茵金勇俊 黄恺女朋友 黄琳达 黄苓舒纹紧致膏 黄苓舒纹紧致膏官网 黄玲演唱会 黄璐百里挑一 黄鳗鱼 黄曼身材 黄美英整容前 黄牛高价炒卖五福卡 黄牛高价炒五福卡 黄绮珊 大爱 黄绮珊 年龄 黄绮珊出轨门 黄绮珊婚姻 黄绮珊离婚 黄绮珊离婚原因 黄绮珊年龄 黄绮珊前夫 黄绮珊为什么离婚 黄鳝钻进王秋玲子宫 黄尚武私房作品合集 黄石一代佳人 黄舒骏微博 黄天决 黄望晴 中国