[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:// ...
随机推荐
- LaTeX 之 \label 的运用
LaTeX 之 \label 的运用 前言 大部分的LaTex教程里面都会提到 \label 的标记功能,而如果入门时就玩耍过WinEdt的同学在工具栏上点击各种环境的时候就会发现\label这个东东 ...
- Codeforces 148B: Escape
题目链接:http://codeforces.com/problemset/problem/148/B 题意:公主从龙的洞穴中逃跑,公主的速度为vp,龙的速度为vd,在公主逃跑时间t时,龙发现公主逃跑 ...
- django-redis 中文文档
Andrey Antukh, niwi@niwi.be 4.7.0 翻译: RaPoSpectre 1. 介绍 django-redis 基于 BSD 许可, 是一个使 Django 支持 Redis ...
- c++ 声明和定义的区别
从编译原理上来说,声明是仅仅告诉编译器,有个某类型的变量会被使用,但是编译器并不会为它分配任何内存.而定义就是分配了内存. int a;在外面是作为一个语句,这就是定义,会构造对象,定义本身也是声明. ...
- java并发之原子性、可见性、有序性
链接:https://blog.csdn.net/gongpulin/article/details/51211616
- 【linux】ls常用参数
1. 命令格式: ls [选项] [目录名] 2. 命令功能: 列出目标目录中所有的子目录和文件. 3. 常用参数: -a, –-all 列出目录下的所有文件,包括以 . 开头的隐含文件 -d, –- ...
- /etc/sysctl.conf参数解释(转)
来自<深入理解Nginx模块开发与架构解析> P9 #表示进程(例如一个worker进程)可能同时打开的最大句柄数,直接限制最大并发连接数fs.file max = 999999 #1代表 ...
- sql server利用cte递归查询
1.数据环境准备 参考Oracle递归查询文章. 2.查询某个节点下的所有子节点 with cte(id,name,parent_id) as ( select id,name,parent_id f ...
- Oracle.DataAccess.dll 部署安装
Oracle.DataAccess.dll 要拷贝到项目发布目录 项目发布的时候,还必须要拷贝以下几个文件在运行目录1.oci.dll 2.oraociicus11.dll 3.OraOps11w.d ...
- Linq模型ObjectContext下查看Sql语句。
ObjectContext 并没有提供 LINQ to SQL DataContext.Log 这样的功能,要查看实际生成的 T-SQL 语句,要么借助 SQL Server Sql Profiler ...