621. Task Scheduler CPU任务间隔分配器
[抄题]:
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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
知道不同字母之间互相制约,但是不知道抓主要矛盾:把字母频次由高到低统计
[一句话思路]:
字母种类少时套公式: 间隔数*(间隔长度+1自身)+ 补齐自身
(max - 1) * (n + 1) + maxNum
,种类多时直接用数组长
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
多个字母都会影响时,选择频率最高的主要影响
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int leastInterval(char[] tasks, int n) {
//cc
//ini: char[26]
int[] letters = new int[26];
int i = 25;
//store in char[26], sort, count frequency
for (char c : tasks) {
letters[c - 'A']++;
}
Arrays.sort(letters);
while (i >= 0 && letters[i] == letters[25]) i--;
//return res
return Math.max(tasks.length, (n + 1) * (letters[25] - 1) + 25 - i);
}
}
621. Task Scheduler CPU任务间隔分配器的更多相关文章
- LeetCode 621. Task Scheduler
原题链接在这里:https://leetcode.com/problems/task-scheduler/description/ 题目: Given a char array representin ...
- 【LeetCode】621. Task Scheduler 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 公式法 日期 题目地址:https://leetco ...
- [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任务调度
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个时间单位.球最少数量的时间单位完成所 ...
- 621. Task Scheduler
https://www.cnblogs.com/grandyang/p/7098764.html 将个数出现最多的那个字符作为分隔的标准,一定是最小的.所以这个时候只需要计算还需要添加多少个idel就 ...
- [leetcode] 621. Task Scheduler(medium)
原题 思路: 按频率最大的字母来分块,频率最大的字母个数-1为分成的块数,每一块个数为n+1 比如AAABBCE,n=2, 则分为A-A- +A AAABBBCCEE,n=2,则分为AB-AB- +A ...
- 使用C#创建计划任务(How to create a Task Scheduler use C# )
本文主要讲解了如何使用C#来创建windows计划任务. 需求:在不定时间段运行多个后台程序(winfrom,wpf,console,等等)用于更新数据. 问题:为什么要使用计划任务,而不直接在程序 ...
- Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析
原文地址: https://blog.csdn.net/yx0628/article/details/80873774 一个简单的Spring定时任务的 demo,全部代码见下载地址:https:// ...
随机推荐
- java少包汇总
1.在下载使用javax.mail的jar包时候,注意: 有的jar没有包含sun的实现,只包含了api,这类jar名称通常为javax.mail-api-x.x.x.jar,在使用smtp协议发邮件 ...
- Python中文报错问题
异常信息:SyntaxError: Non-ASCII character '\xe6' in file D:/pythonlearning/HelloPython.py on line 8, but ...
- awk&sed 小实例
1.打印文件奇数行sed -n 'p;n'sed 'n;d' sed -n '$!N;P'sed -n '1~2p'awk 'i=!i'awk 'NR%2'2.打印文件偶数行sed -n 'n;p's ...
- rapidjson的read和write的sample
头文件 #include "json/document.h" #include "json/prettywriter.h" #include "jso ...
- Java程序员之JS(一) 入门
背景:使用了JS做一个 WEB 项目之后,一直有使用JS 的一个功能,突然某一天项目重新规划,开始自己手动写一些原始JS,情况不妙,原来之前一直是用同事搭建好的框架在开发,对 JS 零基础的我一直在 ...
- tomcat 关闭出现 Connection refused 解决方法
1.找到tomcat占用的端口 ps -eaf|grep tomcat 2.杀掉tomcat进程 kill -p 6453 后记: 杀其他进程的时候,上面的方法不可以,用下面的就ok了 lsof - ...
- java web 程序--注册页面/HashMap的用法。。要懂啊
思路:1.一个form表单,用户输入后,提交 2.第二个是注册页面,主要是用Map.先假设往map里面拿东西,然后判断是否为空 若为空,new 一个HashMap它的子类,然后通过map.conta ...
- java代码--------构造方法的调用
总结: package com.sads; //构造方法何时被调用, //构造方法里的内容先执行 public class Sdw { static { System.out.println(&quo ...
- POJ 3276 Face The Right Way(反转)
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6038 Accepted: 2 ...
- Python添加模块
参考博客:http://blog.csdn.net/damotiansheng/article/details/43916881 http://my.oschina.net/leejun2005/bl ...