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:// ...
随机推荐
- bzoj 2406 矩阵——有源汇上下界可行流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2406 二分答案.把 b 的 n 个行作为一排, m 个列作为一排,每行和每列之间连上下界为 ...
- win7怎么安装和启动 jboss
本文以JBoss Application Server 4.2.1 GA(以下简称JBoss)为例,介绍它在Windows平台上的启动过程.为了方便叙述,对平台环境做以下假定:Java运行时的安装路径 ...
- java指定文件编码格式
在创建文件并打印字符串时,如果不指定编码,默认是按系统的编码格式来.比如我们的linux环境中编码如下: CMREAD-SV43 /home/wlf> locale LANG=en_US.UTF ...
- [转]nginx虚拟目录(alias与root的区别)
今天配置awstats,awstats创建出的文件目录在/home/awstats下,在nginx中加入配置后狂报404,发现还是忽略了root和alias的区别,特将修改配置记录如下: 1.失败:s ...
- couchdb的使用例子
couchdb安装 sudo apt-get install erlang sudo apt-get install libmozjs185-dev libicu-dev 下载源码,编译安装 启动以后 ...
- 关于NHibernate的一些代码
SessionManager using System; using System.IO; using System.Runtime.Serialization; using System.Runti ...
- ASP.NET Redis 开发 入门
ASP.NET Redis 开发 文件并发(日志处理)--队列--Redis+Log4Net Redis简介 Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据 ...
- sprinboot+redis
(1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> < ...
- struts2学习(4)struts2核心知识III
一.result配置: result - name 就是前面返回的值,success,error等. type: dispatcher. 默认是这个,底层是jsp的forward: redirect: ...
- Java 面向对象 初探
public class test { public static void main(String[] args) { // 利用new关键字创建了一个Person对象 Person p = new ...