Task Scheduler
来源:互联网 发布:sql server没有002 编辑:程序博客网 时间:2024/06/01 22:34
题目描述:
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].
分析:用一个数组储存各种任务出现的次数,显然,当会出现休息周期时,所需的最少周期大于字母最大出现次数-1乘以n加一,而每有一个最大出现的字母,我们需要为周期加一,来执行他的最后一个任务。而不出现休息周期,则为任务数本身。
代码如下:
int leastInterval(vector<char>& tasks, int n) { int element[26]={0}; int maxtotal=0; for(int i=0;i<tasks.size();i++){ element[tasks[i]-'A']++; maxtotal=max(maxtotal,element[tasks[i]-'A']); } int answer=(n+1)*(maxtotal-1); for(int i=0;i<26;i++){ if(maxtotal==element[i]) answer++; } if(answer<tasks.size()) return tasks.size(); return answer; }
- Task Scheduler
- Task Scheduler
- Task Scheduler
- Task Scheduler
- Task Scheduler Basic Function
- Windows Scheduler + Java Task
- Task Scheduler 细解
- Task Scheduler 使用
- Task Scheduler I & II
- Task Scheduler接口抽象
- LeetCode 621. Task Scheduler
- 【LeetCode】621. Task Scheduler
- leetcode 621 Task Scheduler
- [Leetcode 621] Task Scheduler
- 621. Task Scheduler
- [LeetCode] 621. Task Scheduler
- Leetcode 621. Task Scheduler
- [leetcode]621. Task Scheduler
- 8. 面向对象 -- 继承
- hdoj-2203亲和串(Kmp)
- 设计模式之建造者模式
- WebStorm开发React项目,修代码之后不更新
- 第8章:指针-指针变量的定义、初始化及解引用(基于哈工MOOC)
- Task Scheduler
- POJ 1088 滑雪
- 网络基础常考面试题--源自牛客网
- Git知识梳理和整合
- BeanUtils将map直接封装到对象中
- h5 canvas画布、setInterval定时器、addEventListener事件
- STL之容器小结
- spring boot日志打印(六)
- 【基本算法】统计n!尾部零