621.任务调度器

任务调度器

给你一个用字符数组 tasks 表示的 CPU 需要执行的任务列表,用字母 A 到 Z 表示,以及一个冷却时间 n。每个周期只能完成一个任务,两个相同种类的任务之间必须有长度为 n 的冷却时间。返回完成所有任务所需要的最短时间。

解析

1
2
3
4
5
6
7
var leastInterval = function (tasks, n) {
const freq = new Array(26).fill(0);
for (const t of tasks) freq[t.charCodeAt(0) - 65]++;
const maxFreq = Math.max(...freq);
const maxCount = freq.filter(f => f === maxFreq).length;
return Math.max(tasks.length, (maxFreq - 1) * (n + 1) + maxCount);
};

公式法:最少时间 = max(任务总数, (最高频率-1) × (n+1) + 最高频率任务数)。


621.任务调度器
https://leetcode.lz5z.com/621.task-scheduler/
作者
tickli
发布于
2024年11月2日
许可协议