作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/task-scheduler/description/

题目描述

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].

题目大意

对很多task进行CPU调度的计算。同样的两个task之间的时间间距不能少于n,否则会用idle填充。问这批task需要多少时间片才能完成。

解题方法

公式法

这个是一个关于CPU调度的问题,刚开始说实话我是很怵这个的,毕竟当年考研机试最后的题目就是CPU调度,我没有做出来。

但是真正做这个题目的时候,发现单个CPU做调度其实很简单。只要知道出现最多的那个(或几个)task就行了,其他的任务往缝隙里面塞。

比如:

  1. 如给定:AAABBCD,n=2。那么我们满足个数最多的任务所需的数量,即可以满足任务间隔要求,即:AXXAXXA;(其中,X表示需要填充任务或者idle的间隔)
  2. 如果有两种或两种以上的任务具有相同的最多的任务数,如:AAAABBBBCCDE,n=3。那么我们将具有相同个数的任务A和B视为一个任务对,最终满足要求的分配为:ABXXABXXABXXAB,剩余的任务在不违背要求间隔的情况下穿插进间隔位置即可,空缺位置补idle。
  3. 由上面的分析我们可以得到最终需要最少的任务时间:(最多任务数-1)*(n + 1) + (相同最多任务的任务个数)

有上面的例子来说就是:(num(A)-1) * (3+1) + (2)。

其中,(最多任务数-1)*(n + 1)代表的是ABXXABXXABXX,(相同最多任务的任务个数)代表的是最后的AB.

最后,别忘了要对任务数求最大值,毕竟每个任务都是要调度一遍的。

代码如下:

class Solution(object):
def leastInterval(self, tasks, n):
"""
:type tasks: List[str]
:type n: int
:rtype: int
"""
count = collections.Counter(tasks)
most = count.most_common()[0][1]
num_most = len([i for i, v in count.items() if v == most])
time = (most - 1) * (n + 1) + num_most
return max(time, len(tasks))

二刷的时候选择的是C++语言,这个公式我很快推导出来了,但是有一点没注意到,就是要返回这个公式求得的时间和任务个数的最大值。

比如对于测试用例["A","A","A","B","B","B"],0,公式求得是(3-1)*(0+1)+2=4,而事实上,任务最少都要被调度一遍的,所以必须对任务个数求个最大值。

为什么会出现这种情况呢?很简单的解释就是当n很小的时候,这个时候n要求两个相同任务调度之间的间隔可以很小,但是事实上,相同任务间隔不可能完全按照n来确定,因为其他的任务也要插在中间。综上,必须要对任务总个数求个最大值。

class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
map<char, int> counter;
for (char task : tasks)
++counter[task];
int most_common = 0;
for (auto t : counter)
most_common = max(most_common, t.second);
int count_most = 0;
for (auto t : counter)
if (t.second == most_common)
count_most ++;
int res = (n + 1) * (most_common - 1) + count_most;
return max(res, int(tasks.size()));
}
};

参考资料:https://blog.csdn.net/Koala_Tree/article/details/78498586

日期

2018 年 8 月 22 日 —— 时不我待
2019 年 1 月 2 日 —— 2019年开刷

【LeetCode】621. Task Scheduler 解题报告(Python & C++)的更多相关文章

  1. [LeetCode] 621. Task Scheduler 任务调度

    Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...

  2. LeetCode 621. Task Scheduler

    原题链接在这里:https://leetcode.com/problems/task-scheduler/description/ 题目: Given a char array representin ...

  3. [LeetCode]621. Task Scheduler 任务安排 题解

    题目描述 给定一个char数组,代表CPU需要做的任务,包含A-Z,不用考虑顺序,每个任务能在1个单位完成.但是有规定一个非负整数n代表两个相同任务之间需要至少n个时间单位.球最少数量的时间单位完成所 ...

  4. [leetcode]621. Task Scheduler任务调度

    Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...

  5. [leetcode] 621. Task Scheduler(medium)

    原题 思路: 按频率最大的字母来分块,频率最大的字母个数-1为分成的块数,每一块个数为n+1 比如AAABBCE,n=2, 则分为A-A- +A AAABBBCCEE,n=2,则分为AB-AB- +A ...

  6. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. Linux内存管理和寻址详解

    1.概念 内存管理模式 段式:内存分为了多段,每段都是连续的内存,不同的段对应不用的用途.每个段的大小都不是统一的,会导致内存碎片和内存交换效率低的问题. 页式:内存划分为多个内存页进行管理,如在 L ...

  2. CAN总线常见的两种编码格式(Intel/Motorola)

    在汽车电子行业的开发或者测试中,我们经常会看到CAN总线信号的常见的两种编码格式:Intel格式与Motorola格式. 讲解这两种格式之前,我们先来了解一些大端模式和小端模式,会对后面理解这两种编码 ...

  3. Flink(二)【架构原理,组件,提交流程】

    目录 一.运行架构 1.架构 2.组件 二.核心概念 TaskManager . Slots Parallelism(并行度) Task .Subtask Operator Chains(任务链) E ...

  4. 使用微信开放标签<wx-open-launch-weapp>的踩坑日记

    最近在完成H5跳转小程序需求时,使用到了微信官方退出的开放标签<wx-open-launch-weapp>,来谈一谈使用的心得和不足. 1.适用环境微信版本要求为:7.0.12及以上. 系 ...

  5. Spring同一个类中的注解方法调用AOP失效问题总结

    public interface XxxService { // a -> b void a(); void b(); } @Slf4j public class XxxServiceImpl ...

  6. oracle异常处理——ORA-01000:超出打开游标最大数

    oracle异常处理--ORA-01000:超出打开游标最大数https://www.cnblogs.com/zhaosj/p/4309352.htmlhttps://blog.csdn.net/u0 ...

  7. Linux基础命令---slabtop

    slabtop slabtop实时显示详细的内核板条缓存信息.它显示按所列排序条件之一排序的顶级缓存的列表.它还会显示一个统计信息头,其中填充了板坯层信息. 此命令的适用范围:RedHat.RHEL. ...

  8. Springboot Oauth2 集成Swagger2权限验证实战

    Swagger是什么?能干什么?在这就不展开讲解了.本文主要讲解如何集成OAuth2的Password模式权限验证,验证接口是否具有权限. 引入依赖 <dependency> <gr ...

  9. vue-cli2嵌入html

    1.使用iframe <!-- 相对路径/绝对路径 --> <iframe src="../../../static/zsw.html"></ifra ...

  10. Linux(CentOS 7)使用gcc编译c,c++代码

    安装gcc: 1.使用 yum -list gcc* 查询 centos 官方gcc的所有包: 可安装的软件包 gcc.x86_64 gcc-c++.x86_64 gcc-gfortran.x86_6 ...