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

最新文章

  1. 发布新款博客皮肤SimpleMemory
  2. word-wrap和word-break的区别
  3. 论文第5章:Android绘图平台的实现
  4. 1009-2的N次方
  5. SSRS和SSAS是支持VB的
  6. 14.5.5 Deadlocks in InnoDB
  7. editplus 使用小技巧
  8. Win32 API中的user32.dll中的ShowWindow方法参数整理
  9. Android性能优化典范【转】
  10. 1!到n!的和
  11. margin叠加相邻两个元素的上下margin是叠加在一起
  12. Android ui 透明度设置
  13. ATM+购物商城完整版
  14. File文件操作学习总结
  15. opencv学习之路(39)、PCA
  16. oracle判断是否包含字符串的方法
  17. array_walk与array_map的区别
  18. String的substring方法
  19. Mybatis-generator生成Service和Controller
  20. 腾讯微信被怼,iOS版微信不能打赏了

热门文章

  1. Spring 之定义切面尝试(基于 XML)
  2. 阿里云服务器: centos7 ftp安装
  3. 20162305李昱兴 2016-2017-2 《Java程序设计》第2周学习总结
  4. OpenGL核心技术之Gamma校正
  5. ReactNative之Android打包APK方法(趟坑过程)
  6. tomcat启动时出现There are no resources that can be added or removed from the server
  7. codeforces 703B
  8. CentOS 5 上使用yum同时安装32位和64位包的解决方法
  9. 数字组合问题:Combination,CombinationSum,CombinationSum2,CombinationSum3
  10. Linux 增加对外开放的端口