Greedy- 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 1:
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
Note:
- The number of tasks is in the range [1, 10000].
- The integer n is in the range [0, 100].
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
int ret=;
if(n== || tasks.size()<) return tasks.size();
vector<int> cnt(,); // A~Z
for(auto a:tasks) //统计 每种task的数量
++cnt[a-'A'];
while(){
//[](int a, int b){return a>b;} 是个lambda表达式, 让排序结果为降序排列
sort(cnt.begin(),cnt.end(),
[](int a, int b){return a>b;});
if( n> || cnt[n]==) //是否为第二种情况
break;
ret+=(n+);
for(size_t i=; i<n+; ++i)
--cnt[i];
}
//第二种情况的计算
if(cnt[]!=){
ret+=(cnt[]-)*(n+); // 要减1,见第二种情况示例
for(int i=; i<;++i)
if(cnt[i]==cnt[]) ++ret;
}
return ret;
}
};
Greedy- 621. Task Scheduler的更多相关文章
- 621. Task Scheduler
https://www.cnblogs.com/grandyang/p/7098764.html 将个数出现最多的那个字符作为分隔的标准,一定是最小的.所以这个时候只需要计算还需要添加多少个idel就 ...
- [leetcode]621. Task Scheduler任务调度
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...
- 621. Task Scheduler CPU任务间隔分配器
[抄题]: Given a char array representing tasks CPU need to do. It contains capital letters A to Z where ...
- LeetCode 621. Task Scheduler
原题链接在这里:https://leetcode.com/problems/task-scheduler/description/ 题目: Given a char array representin ...
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 公式法 日期 题目地址:https://leetco ...
- [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 ...
- Windows Task Scheduler Fails With Error Code 2147943785
Problem: Windows Task Scheduler Fails With Error Code 2147943785 Solution: This is usually due to a ...
- 在Windows Server 2012的Task Scheduler里面配置自动发送邮件
最近在一台server上配置了每个周末备份数据库的定时任务,想顺手配置发送备份完成的邮件提醒我去Double Check一下备份结果. 悲剧地发现Send an email功能被新版的server给禁 ...
随机推荐
- jQuery 插件使用记录
Validate 表单验证 ver 1.6 浏览更多 默认情况下,当表单 submit 时,那些验证不通过的 field 旁边会出现错误消息提示,有时很方便,但有时看起来很不美观.可以关闭此消息提示. ...
- 跳出思维定势,改变交谈习惯zz
一直以来我都是一个不折不扣的作者所划分的内向者,羞于在公众场合说话,也不愿意与陌生人交谈,甚至是与认识的人聊天,有时候也是一种痛苦,看着在办公室里夸夸其谈的同事们,我总是感觉格格不入.严格说来,我算是 ...
- 2018.06.30 BZOJ1026: [SCOI2009]windy数(数位dp)
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MB Description windy定义了一种windy数.不含前导零且相邻两 ...
- 第1章 (名词)Le nom
★名词的种类:(1)普通名词 —专有名词,如: un livre —la Chine(2)可数名词—不可数名词,如: un ami —le lait(3)具体名词— ...
- Navicat for oracle cannot load OCI DLL
Navicat for oracle 提示 cannot load OCI DLL87,126,193 instant client package is required for basic and ...
- Windows 下安装mysql总结
1.配置环境变量 将安装目录添加到系统路径 我的电脑->属性->高级->环境变量->path 2.修改my.ini 位于解压安装目录下 在其中修改或添加配置: [mysqld] ...
- MySQL终端下常用命令
一:控制类命令 1.show variables like "%datadir%";显示注册在variables中(一个注册表key-value的格式存储数据)key能匹配%dat ...
- 链家笔试链家——找寻最小消费获取最大平均分java
链家找寻最小消费获取最大平均分 输入: 5 5 4#表示科目数n,每科最大分值r,平均分avg 5 2#每科的实际得分,分数加1分的消耗的能量 4 7 3 1 3 2 2 5 输出: 4 #到达n*a ...
- php读取用友u8客户档案
include('../common/conn.php'); $list=[]; $sql="SELECT a.cCusCode,a.cCusName,b.cCCName,a.cCusDep ...
- ZOJ2418 Matrix 2017-04-18 21:05 73人阅读 评论(0) 收藏
Matrix Time Limit: 2 Seconds Memory Limit: 65536 KB Given an n*n matrix A, whose entries Ai,j a ...