621. Task Scheduler
来源:互联网 发布:057188158198是淘宝吗 编辑:程序博客网 时间:2024/06/06 00:47
原题:
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 twosame 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 = 2Output: 8Explanation: 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].
思考过程&解题思路:一开始没什么好的思路,借鉴了别人的思路,受了一点启发,最后自己完成。先把出现次数最多的字母排好,其它字母往里插入。最后有两种情况:插入之后仍有空缺,则返回值是(n + 1) * (k - 1) + j。其中k是出现次数最多的字母的出现次数,j是出现次数为k的字母个数;都插满之后仍然有字母剩余,那么剩余字母总可以无缝插进去,这时候返回值是tasks.length。
结果代码:
public int leastInterval(char[] tasks, int n) { int[] alphabet = new int[26];//int[n]的值表示第n + 1个字母在tasks里出现的次数 int len = tasks.length; for (int i = 0;i < len;i++) alphabet[tasks[i] - 'A']++; Arrays.sort(alphabet); int i = 1,max = alphabet[25];//max是出现次数最大值 while (alphabet[25 - i] == alphabet[25]) i++;//看看有几个最大值,这会影响结果 return Math.max((n + 1) * (max - 1) + i,len); }
阅读全文
0 0
- LeetCode 621. Task Scheduler
- 【LeetCode】621. Task Scheduler
- 621. Task Scheduler
- [LeetCode] 621. 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
- 621. Task Scheduler
- 621. Task Scheduler
- 621. Task Scheduler
- leetcode 621. Task Scheduler
- Leetcode 621. Task Scheduler
- leetcode 621. Task Scheduler
- 621. Task Scheduler
- 621. Task Scheduler--任务调度
- 线性表的链式表示和实现
- linux 后台运行命令 nohup命令
- MySQL免安装版中 my-default.ini 的配置
- python中关于dir()
- Spring AOP原理 AOP两种配置方式
- 621. Task Scheduler
- java Collections工具类使用总结(疯狂java讲义读书笔记)
- RxAndroid的环境搭建(二)
- quanxian
- 算法导论之链表
- 还是httpurlconnection
- 自定义View 圆形进度条
- Retrofit网络请求和MVP的简单的讲解
- 易学MVP 个络加载数据+XRecyclerView+Retrofit