原题链接在这里: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].

题解:

类似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的更多相关文章

  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 任务安排 题解

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

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

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

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

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

  5. 【LeetCode】621. Task Scheduler 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 公式法 日期 题目地址:https://leetco ...

  6. 621. Task Scheduler

    https://www.cnblogs.com/grandyang/p/7098764.html 将个数出现最多的那个字符作为分隔的标准,一定是最小的.所以这个时候只需要计算还需要添加多少个idel就 ...

  7. 621. Task Scheduler CPU任务间隔分配器

    [抄题]: Given a char array representing tasks CPU need to do. It contains capital letters A to Z where ...

  8. Windows Task Scheduler Fails With Error Code 2147943785

    Problem: Windows Task Scheduler Fails With Error Code 2147943785 Solution: This is usually due to a ...

  9. 在Windows Server 2012的Task Scheduler里面配置自动发送邮件

    最近在一台server上配置了每个周末备份数据库的定时任务,想顺手配置发送备份完成的邮件提醒我去Double Check一下备份结果. 悲剧地发现Send an email功能被新版的server给禁 ...

随机推荐

  1. HTML5/CSS3图片左右切换弹性动画

    在线演示 本地下载

  2. 使用AutoIT检测已安装软件,并将结果保存在桌面

    $file = "\adobe安装列表.txt" $regedit1 = "hklm64\SOFTWARE\Wow6432Node\Microsoft\Windows\C ...

  3. Java学习之垃圾回收

    垃圾回收(GC) GC需要完成的三件事情: 哪些内存需要回收? 什么时候回收? 如何回收? 为什么"GC自动化"之后还要研究GC?当需要排查各种内存溢出.内存泄漏问题时,当GC成为 ...

  4. 深入Jetty源码之Servlet框架及实现(Servlet、Filter、Registration)

    概述 Servlet是Server Applet的缩写,即在服务器端运行的小程序,而Servlet框架则是对HTTP服务器(Servlet Container)和用户小程序中间层的标准化和抽象.这一层 ...

  5. Pandas选项和自定义

    Pandas提供API来自定义其行为的某些方面,大多使用来显示. API由五个相关函数组成.它们分别是 - get_option() set_option() reset_option() descr ...

  6. scjp考试准备 - 8 - final关键字

    题目,如下代码的执行结果: import java.util.ArrayList; class Pizza{ ArrayList toppings; public final void addTopp ...

  7. js 格式化时间日期函数小结3

    function DateUtil(){}/***功能:格式化时间*示例:DateUtil.Format("yyyy/MM/dd","Thu Nov 9 20:30:37 ...

  8. saltstack学习篇

    参考链接:http://sofar.blog.51cto.com/353572/1596960/ http://sofar.blog.51cto.com/353572/1596960/ 自动化运维工具 ...

  9. 【OpenGL ES】关于VBO(Vertex Buffer Object)的一些坑——解析一些关于glBuffer的函数

    最近在写毕设的时候用到OpenGL ES中的VBO,由于对一些接口用到的变量不了解被坑得很惨,在此记录一下防止以后再被坑. 本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cn ...

  10. Superset 初探

    安装都是借鉴的别人的,已经剪裁下来.到自己文件夹里了. 下面介绍.如何启动superset ,BI 分析工具.这是我以前的强项.应该没问题. 问题: 安装好了之后,再打开localhost 就拒绝访问 ...