LeetCode 621. Task Scheduler
原题链接在这里: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].
题解:
类似Rearrange String k Distance Apart.
想要最少的interval完成所有task, 需要先去完成frequency最高的task.
用maxHeap找出frequency 高的task.
然后挨个去执行, 把执行过的task frequency -1.
同时间隔n也递减. 如果n减为0. 说明间隔达到要求, 可以把执行过的task 如果frequency 还是大于0的放回到maxHeap中.
如果n还没尖刀0, maxHeap就空了, 就需要idle来补位. 之后再把执行过的task 如果frequency 还是大于0的放回到maxHeap中.
Time Complexity: O(task.length). 每个task都执行了一遍, 中间多出的idle interval都是通过计算一次加进count中. 最多加最大frequency次idle interval 进count.
Space: O(1). map的size最大26.
AC Java:
class Solution {
public int leastInterval(char[] tasks, int n) {
if(tasks == null || tasks.length == 0){
return 0;
}
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
for(char c : tasks){
hm.put(c, hm.getOrDefault(c,0)+1);
}
PriorityQueue<Map.Entry<Character, Integer>> maxHeap = new PriorityQueue<Map.Entry<Character, Integer>>(
(a, b) -> b.getValue() - a.getValue()
);
maxHeap.addAll(hm.entrySet());
int count = 0;
while(!maxHeap.isEmpty()){
LinkedList<Map.Entry<Character, Integer>> que = new LinkedList<Map.Entry<Character, Integer>>();
int k = n+1;
while(k > 0 && !maxHeap.isEmpty()){
Map.Entry<Character, Integer> cur = maxHeap.poll();
cur.setValue(cur.getValue()-1);
que.add(cur);
k--;
count++;
}
for(Map.Entry<Character, Integer> entry : que){
if(entry.getValue() > 0){
maxHeap.add(entry);
}
}
if(maxHeap.isEmpty()){
break;
}
count += k; // k != 0 要添加idle interval
}
return count;
}
}
LeetCode 621. Task Scheduler的更多相关文章
- [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 任务安排 题解
题目描述 给定一个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】621. Task Scheduler 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 公式法 日期 题目地址:https://leetco ...
- 621. Task Scheduler
https://www.cnblogs.com/grandyang/p/7098764.html 将个数出现最多的那个字符作为分隔的标准,一定是最小的.所以这个时候只需要计算还需要添加多少个idel就 ...
- 621. Task Scheduler CPU任务间隔分配器
[抄题]: Given a char array representing tasks CPU need to do. It contains capital letters A to Z where ...
- Windows Task Scheduler Fails With Error Code 2147943785
Problem: Windows Task Scheduler Fails With Error Code 2147943785 Solution: This is usually due to a ...
- 在Windows Server 2012的Task Scheduler里面配置自动发送邮件
最近在一台server上配置了每个周末备份数据库的定时任务,想顺手配置发送备份完成的邮件提醒我去Double Check一下备份结果. 悲剧地发现Send an email功能被新版的server给禁 ...
随机推荐
- MySQL引擎及选择
一.MySQL的存储引擎 完整的引擎说明还是看官方文档:http://dev.mysql.com/doc/refman/5.6/en/storage-engines.html 这里介绍一些主要的引擎 ...
- vue切换路由模式{hash/history}
vue中常用的路由模式 hash(#):默认路由模式 histroy(/)切换路由模式 切换路由模式 export default new Router({ // 路由模式:hash(默认),hist ...
- Go语言学习之常量(The way to go)
生命不止,继续go go go . 上一篇博客<Go语言学习之变量(The way to go)介绍了go中的变量,今天就介绍常量. const关键字 跟c++中一样,go中同样具有const关 ...
- CentOS6、7优化脚本完美版
#!/bin/bash SysVer=`cat /etc/redhat-release | awk -F'release' '{print $2}' | awk -F'[ .]+' '{print $ ...
- Latex排版全解【转载】
Latex排版全解 https://www.cnblogs.com/jingwhale/p/4250296.html
- MySQL的索引实现原理
MySQL数据库索引总结使用索引的原由数据结构Hash.平衡二叉树.B树.B+树区别机械硬盘.固态硬盘区别Myisam与Innodb B+树的区别MySQL中的索引什么数据结构B+树中的节点到底存放多 ...
- Java中Collections.sort()排序详解
public static void main(String[] args) { List<String> list = new ArrayList<String>(); ...
- NumPy Matplotlib库
NumPy - Matplotlib Matplotlib 是 Python 的绘图库. 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案. 它也可以和图形工具包一起使用,如 ...
- Object有哪些方法?
有9个方法 1 clone 2 toString() 3 getClass 4 finalize 5 equals 6 hascode 7 notify 8 notifall 9 wait
- swagger 文件上传
单个文件上传的写法: @RequestParam MultipartFile pictures 多个文件上传的写法: @RequestParam String token,@RequestPart(& ...