[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:// ...
随机推荐
- Field 'id' doesn't have a default value 原因
Field 'id' doesn't have a default value昨晚做项目的时候遇到一个问题,在测试数据存储的时候老是报Field 'id' doesn't have a default ...
- I.MX6 Manufacturing Tool V2 (MFGTool2) Update Command List (UCL) User Guide translate
Manufacturing Tool V2 (MFGTool2) Update Command List (UCL) User Guide Contents(目录) Contents(目录) ...
- 在Android中来修改SQL里面的权限和显示内容
1.在Android中建立了一个数据库. 然后要知道存储数据库的路劲.得到路劲然后进入cmd里面进到 手机终端. 2.利用splite3来显示数据库里面的东西. 3.利用chmod来修改数据库文件的权 ...
- [C#] DataTable转成List集合
项目开发中,经常会获取到DataTable对象,如何把它转化成一个List对象呢?前几天就碰到这个问题,网上搜索整理了一个万能类,用了泛型和反射的知识.共享如下: public class Model ...
- linux下不同tomcat使用不同的jdk版本
环境:Linux tomcat7 使用jdk7 tomcat 8使用jdk8 最近写好了一个程序,需要部署在测试机器上,这个测试机器上已经有了在运行的tomcat7, 我这个程序要求使用tomcat8 ...
- visual studio内置“iis”组件提取及二次开发
简介 visual studio安装后会自带小型的“iis”服务器,本文就简单提取一下这个组件,自己做一个小型“iis”服务器吧.先来说用途吧(废话可绕过),比如在服务器上没有安装iis,或者给客户演 ...
- linux用命令行看网线物理连接状态
inux下有两个命令可以看网线状态: ethtool 和 mii-tool 竟然还都要用,由于网卡所支持的协议不同,哪个网卡能用哪个命令是不一定的. [root@rhel demo]# ethtool ...
- ASP.NET 迭代控件获得行号
如何获取Repeater的当前行号,其实Repeater自身就带有这个获取当前行号的属性,而无需程序员绑定这个行号.到底要怎么实现呢?其实使用Repeater中的 Container.ItemInde ...
- linux Posix 信号量 三 (经典例子)
本文将阐述一下信号量的作用及经典例子,当中包括“<越狱>寄信”,“家庭吃水果”,“五子棋”,“接力赛跑”,“读者写者”,“四方恋爱”等 首先,讲 semWait操作(P操作)和semSig ...
- unity里面的gameobject和transform的关系
一切都是物体(gameobject),而transform是物体的一个基本属性类,包含位置,旋转,缩放,三个基本属性,两者之间可以互相转换 查找物体,建议用transform,GameObject无法 ...