[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 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].
一个表示CPU要做的任务数组。用大写字母A到Z来表示,不同的字母表示不同的任务。任务完成顺序不限,每个任务可在一个时间内完成,对于每个时间,CPU可以完成一个任务或空闲。但在两个相同任务之间要有一个冷却时间间隔n,间隔内CPU可以执行其他不同的任务或是处于空闲状态。求CPU完成所有给定任务所需的最小时间。
解法:先用哈希表或者数组对任务出现的次数进行统计,找出任务出现次数的最大值max_count(有可能是多个),然后把这个任务(或者任务对)之间按n个间隔进行分隔排列,然后把其它任务插到这些间隔里面。由于其它任务出现的次数少于max_count所以肯定能放下,如有空位就是需要空闲(idle)。最后,还要注意的是,如果空位都插满之后还有任务,那就随便在这些间隔里面插入就可以,因为间隔长度肯定会大于n,在这种情况下就是任务的总数是最小所需时间。
情况1:AAABBCD,n=2。分隔为:AXXAXXA -> ABCABDA(其中,X表示需要填充任务或者idle的间隔)
情况2:AAAABBBBCCDE,n=3。分隔为:ABXXABXXABXXAB -> ABCDABCEABXXAB 或者 ABCXABCXABDEAB, etc. 此时要加上出现次数也是max_count的任务的个数same_max_count(相当于末尾多了这几个)。
情况3:AAABBCDEF,n=2。分隔为:AXXAXXA -> ABCEABDFA 或者 ABCEABDAF, etc. 此时就是task的长度是最小时间。
最少的任务时间公式:max(len(tasks), (max_count-1) * (n+1) + other_max_count)
Java: O(1) time O(1) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Solution { public int leastInterval( char [] tasks, int n) { int [] counter = new int [ 26 ]; int max = 0 ; int maxCount = 0 ; for ( char task : tasks) { counter[task - 'A' ]++; if (max == counter[task - 'A' ]) { maxCount++; } else if (max < counter[task - 'A' ]) { max = counter[task - 'A' ]; maxCount = 1 ; } } int partCount = max - 1 ; int partLength = n - (maxCount - 1 ); int emptySlots = partCount * partLength; int availableTasks = tasks.length - max * maxCount; int idles = Math.max( 0 , emptySlots - availableTasks); return tasks.length + idles; } } |
Java: O(N) time O(26) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // (c[25] - 1) * (n + 1) + 25 - i is frame size // when inserting chars, the frame might be "burst", then tasks.length takes precedence // when 25 - i > n, the frame is already full at construction, the following is still valid. public class Solution { public int leastInterval( char [] tasks, int n) { int [] c = new int [ 26 ]; for ( char t : tasks){ c[t - 'A' ]++; } Arrays.sort(c); int i = 25 ; while (i >= 0 && c[i] == c[ 25 ]) i--; return Math.max(tasks.length, (c[ 25 ] - 1 ) * (n + 1 ) + 25 - i); } } |
Python: O(1) time O(1) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution( object ): def leastInterval( self , tasks, n): """ :type tasks: List[str] :type n: int :rtype: int """ count = collections.defaultdict( int ) max_count = 0 for task in tasks: count[task] + = 1 max_count = max (max_count, count[task]) result = (max_count - 1 ) * (n + 1 ) for count in count.values(): if count = = max_count: result + = 1 return max (result, len (tasks)) |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution { public : int leastInterval(vector< char >& tasks, int n) { int mx = 0, mxCnt = 0; vector< int > cnt(26, 0); for ( char task : tasks) { ++cnt[task - 'A' ]; if (mx == cnt[task - 'A' ]) { ++mxCnt; } else if (mx < cnt[task - 'A' ]) { mx = cnt[task - 'A' ]; mxCnt = 1; } } int partCnt = mx - 1; int partLen = n - (mxCnt - 1); int emptySlots = partCnt * partLen; int taskLeft = tasks.size() - mx * mxCnt; int idles = max(0, emptySlots - taskLeft); return tasks.size() + idles; } }; |
类似题目:
[LeetCode] 358. Rearrange String k Distance Apart 按距离k间隔重排字符串
All LeetCode Questions List 题目汇总
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具