Task Scheduler
来源:互联网 发布:apache压缩包下载 编辑:程序博客网 时间:2024/06/07 22:02
题目描述
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.
思路分析
这题目可以用贪婪算法实现,基本思想是:先计算出每个task出现的频数存入number容器中,再将所有频数按降序调用sort排好序后存入queue容器中,并计算出现频数最高的task的种类Largest。将频数最高的task按照间隙分布,于是会有(queue[0]-1)个间隙,再往间隙中插入频数递减的task,每次在一个间隙中插入一个task,从左往右循环插入,可分为两种情况讨论:
task可以填充时间间隙,则CPU所花的最短时间是task的个数;
task不能完全填充时间间隙,需要补充idle,则CPU所花的最短时间是(queue[0]-1)*(n+1) + Largest
代码实现
class Solution {public: static bool compare(const int& a, const int& b) { if (a >= b) return true; else return false; } int leastInterval(vector<char>& tasks, int n) { int len = tasks.size(); std::vector<int> number(26, 0); //记录每个task出现的频数 std::vector<int> queue; //按频数大小排序 for (int i = 0; i < len; i++) { number[tasks[i]-'A'] += 1; } for (int i = 0; i < 26; i++) { if (number[i] != 0) queue.push_back(number[i]); } sort(queue.begin(), queue.end(), compare); int Largest = 0; //最高频数的task类型数 for (int i = 0; i < queue.size(); i++) { if (queue[i] == queue[0]) Largest += 1; else break; } int count = 0; //总时间数 //除去频数最高的tasks,剩余tasks不能补全时间间隔 int temp = (queue[0]-1)*(n+1) + Largest; if (len >= temp) count = len; else count = temp; return count; }};
- 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
- 今年最新的30个Android库
- BurpSuite安全测试
- ANDROID样式的开发:LAYER-LIST篇
- Java线程(一):synchronized
- 修改hosts不必重启 立刻生效
- Task Scheduler
- AlphaGo Zero的伟大与局限—ResNet作者、旷视研究院院长孙剑深度评述
- 一文了解机器学习开发工具
- 掘金3000亿在线教育市场 ‖ 沪江英语、流利说、Master Learner如何用AI影响1400万教师和1.88亿学生
- 一些JAVA代码(单词长度)
- 安卓常用工具类-ToastUtils【提示工具类】
- dedecms相关文档调用方法
- 数据库索引基础知识整理
- tensorflow 笔记(1)