[leetcode]621. Task Scheduler任务调度
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:
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
代码
class Solution {
public int leastInterval(char[] tasks, int n) {
// corner
if( tasks == null || tasks.length == 0 ) return 0;
// simplified edition of hashmap
int []map = new int[26];
//remark every character's frequency
for(int i = 0; i< tasks.length; i++){
map[tasks[i]-'A']++;
}
int max = 0;
int c = 0;
//找到频率最高的
// find the toppest frequency
for(int i = 0; i< 26; i++){
max = Math.max(max, map[i]); // update the max while traversing
}
//频率最高的可能被安排到最后
// maybe there is not only one toppest frequency
for(int i = 0; i< 26; i++){
if(max == map[i]){
c++;
}
}
int ans = (n+1) * (max-1) + c;
/* there is other possibility that we can schedule all the tasks without idling , then the max would be tasks.length */
return Math.max(tasks.length, ans);
}
}
[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
原题链接在这里:https://leetcode.com/problems/task-scheduler/description/ 题目: Given a char array representin ...
- [LeetCode]621. Task Scheduler 任务安排 题解
题目描述 给定一个char数组,代表CPU需要做的任务,包含A-Z,不用考虑顺序,每个任务能在1个单位完成.但是有规定一个非负整数n代表两个相同任务之间需要至少n个时间单位.球最少数量的时间单位完成所 ...
- [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 ...
- SpringBoot2 task scheduler 定时任务调度器四种方式
github:https://github.com/chenyingjun/springboot2-task 使用@EnableScheduling方式 @Component @Configurabl ...
- Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析
原文地址: https://blog.csdn.net/yx0628/article/details/80873774 一个简单的Spring定时任务的 demo,全部代码见下载地址:https:// ...
随机推荐
- bacnet ip转MQTT
迈思德网关最新支持BACNET IP协议,可以将BACNET IP转换为MODBUS.MQTT.OPC等协议,与百度天工,阿里云等无缝对接. 支持AI.AO.DI.DO.AV.DV六个对象的读写.
- Zabbix监控TCP status
监控原理 ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' LAST-ACK 5ESTAB 348FIN-WAIT-1 ...
- Python数据类型-02.字符串
本文主要记录字符串的相关知识,包括字符串的定义特点,常用方法和 请知悉: 计算机中,一切皆为对象世界万物,皆为对象,一切对象皆可分类 1.什么是字符串? 类似"hello world&quo ...
- BZOJ2938: [Poi2000]病毒(AC自动机)
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1678 Solved: 849[Submit][Status][D ...
- Spring Boot 的项目打包成的 JAR 包,制作成 docker 镜像并运行
上一篇:Docker学习(三)docker容器操作 首先把本地的项目打包好,我这里直接把已经打包好的springboot-mybatis-0.0.1-SNAPSHOT.jar包直接上传到linuxmy ...
- .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
.NET 中提供了很多判断某个类型或实例是某个类的子类或某个接口的实现类的方法,然而这事情一旦牵扯到泛型就没那么省心了. 本文将提供判断泛型接口实现或泛型类型子类的方法. 本文内容 .NET 中没有自 ...
- 《DSP using MATLAB》示例Example 10.4
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Linux内核静态映射表的建立过程
/* * 平台: s5pv210 * 内核版本号: 2.6.35.7 */ kernel/arch/arm/mach-s5pv210/mach-smdkc110.c 这个文件是由三星在 ...
- 一步搞定私有Git服务器部署(Gogs)
http://www.jianshu.com/p/424627516ef6 零.安装 Docker 和 Compsoe 首先安装 Docker: $ curl -sSL https://get.doc ...
- PhpStorm 10.0.3 下载安装与汉化
https://www.7down.com/soft/229568.html 2JA97R55MG-eyJsaWNlbnNlSWQiOiIySkE5N1I1NU1HIiwibGljZW5zZWVOYW ...