【LeetCode】621. Task Scheduler 解题报告(Python & C++)
作者: 负雪明烛
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:
- The number of tasks is in the range [1, 10000].
- The integer n is in the range [0, 100].
题目大意
对很多task进行CPU调度的计算。同样的两个task之间的时间间距不能少于n,否则会用idle填充。问这批task需要多少时间片才能完成。
解题方法
公式法
这个是一个关于CPU调度的问题,刚开始说实话我是很怵这个的,毕竟当年考研机试最后的题目就是CPU调度,我没有做出来。
但是真正做这个题目的时候,发现单个CPU做调度其实很简单。只要知道出现最多的那个(或几个)task就行了,其他的任务往缝隙里面塞。
比如:
- 如给定:AAABBCD,n=2。那么我们满足个数最多的任务所需的数量,即可以满足任务间隔要求,即:AXXAXXA;(其中,X表示需要填充任务或者idle的间隔)
- 如果有两种或两种以上的任务具有相同的最多的任务数,如:AAAABBBBCCDE,n=3。那么我们将具有相同个数的任务A和B视为一个任务对,最终满足要求的分配为:ABXXABXXABXXAB,剩余的任务在不违背要求间隔的情况下穿插进间隔位置即可,空缺位置补idle。
- 由上面的分析我们可以得到最终需要最少的任务时间:(最多任务数-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++)的更多相关文章
- [LeetCode] 621. Task Scheduler 任务调度
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...
- LeetCode 621. Task Scheduler
原题链接在这里:https://leetcode.com/problems/task-scheduler/description/ 题目: Given a char array representin ...
- [LeetCode]621. Task Scheduler 任务安排 题解
题目描述 给定一个char数组,代表CPU需要做的任务,包含A-Z,不用考虑顺序,每个任务能在1个单位完成.但是有规定一个非负整数n代表两个相同任务之间需要至少n个时间单位.球最少数量的时间单位完成所 ...
- [leetcode]621. Task Scheduler任务调度
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...
- [leetcode] 621. Task Scheduler(medium)
原题 思路: 按频率最大的字母来分块,频率最大的字母个数-1为分成的块数,每一块个数为n+1 比如AAABBCE,n=2, 则分为A-A- +A AAABBBCCEE,n=2,则分为AB-AB- +A ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- PPT插件——iSlide全功能解析
做幻灯展示是我们日常工作中不可缺少的一部分,有的人喜欢用代码如Latex, markdown+pandoc+revealjs 或 bookdown.这些都是自动化做幻灯的好工具.我也都有过体会,确实简 ...
- 【.Net】使用委托实现被引用的项目向上级项目的消息传递事件
前言:在实际项目过程中,经常可能遇到被引用的项目要向上传递消息,但是又不能通过方法进行返回等操作,这个时候委托就派上用场了.以下使用委托,来实现被引用的项目向上传递消息的小教程,欢迎各位大佬提供建议. ...
- ORACLE中dual用法详解
基本上oracle引入dual为的就是符合语法1. 我们先从名称来说,dual不是缩写词,本身就是完整的单词.dual名词意思是对数,做形容词时是指二重的,二元的.2. Oracle中的dual表是一 ...
- Running shell commands by C++
#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; st ...
- JDK1.8新特性(一): 接口的默认方法default
前言 今天在学习mysql分区优化时,发现一个博客专家大神,对其发布的文章简单学习一下: 一:简介 我们通常所说的接口的作用是用于定义一套标准.约束.规范等,接口中的方法只声明方法的签名,不提供相应的 ...
- mysql锁相关讲解及其应用
一.mysql的锁类型 了解Mysql的表级锁 了解Mysql的行级锁 (1) 共享/排它锁(Shared and Exclusive Locks) 共享锁和排他锁是InnoDB引擎实现的标准行级别锁 ...
- 【Linux】【Shell】【Basic】Programming
shell脚本编程: 编程语言的分类:根据运行方式 编译运行:源代码-->编译器(编译)-->程序文件 解释运行:源代码-->运行时启动解释器,又解释器边解释边运行 根据其编程过程中 ...
- 编译安装haproxy2.0
先解决lua环境,(因为centos自带的版本不符合haproxy要求的最低版本(5.3)先安装Lua依赖的包 [root@slave-master lua-5.3.5]# yum install ...
- jQuery节点更新
一.插入子节点 var $newNode1 = $("<p>我是p标签</p>"); 加入之后,原来的会删除. 二.插入兄弟节点 三.替换节点 1.HTML ...
- 【C/C++】从矩阵左上角走到右下角
tx的笔试,但是只过了10%,就离谱 #include <bits/stdc++.h> using namespace std; const int maxn = 1010; long d ...