Loading

[LeetCode] 621. Task Scheduler(任务调度器)

Description

Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.
给定一个字符数组 tasks 表示 CPU 需要完成的任务,每个字母代表不同的任务。任务可以以任意顺序完成。每个任务需花费 1 个单位的时间完成。对于每一个时间单位,CPU 要么完成一个任务,要么处于空闲状态

However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.
然而,存在一个非负整数 n,其表示两个相同任务(数组里两个相同字母)之间的 CD,也就是说,完成两件相同的任务,之间需要至少间隔 n 个时间单位。

Return the least number of units of times that the CPU will take to finish all the given tasks.
返回 CPU 完成给定的所有任务所需花费的最少时间。

Examples

Example 1

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: 
A -> B -> idle -> A -> B -> idle -> A -> B
There is at least 2 units of time between any two same tasks.

Example 2

Input: tasks = ["A","A","A","B","B","B"], n = 0
Output: 6
Explanation: On this case any permutation of size 6 would work since n = 0.
["A","A","A","B","B","B"]
["A","B","A","B","A","B"]
["B","B","B","A","A","A"]
...
And so on.

Example 3

Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output: 16
Explanation: 
One possible solution is
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A

Constraints

  • 1 <= task.length <= 10^4

  • tasks[i] is upper-case English letter.

  • The integer n is in the range [0, 100]

Solution

虽然能够想到优先安排频率最高的任务,然后在这之间插空,但转化不成代码也是个头痛的事。以下代码来自 discussion,唯一一处不理解的地方在于最后 idles 的计算

import kotlin.math.max

class Solution {
    fun leastInterval(tasks: CharArray, n: Int): Int {
        val counter = hashMapOf<Char, Int>()
        // 出现频率最多的任务的次数
        var max = 0
        // 出现频率最多的任务的个数
        var maxCount = 0
        // 以上两个结合起来看,就是以下意思:
        // 任务中有 ${maxCount} 个任务出现的频率最高,各要执行 ${max} 次

        for (task in tasks) {
            counter[task] = counter.getOrDefault(task, 0) + 1
            if (max == counter.getValue(task)) {
                maxCount++
            } else if (max < counter.getValue(task)) {
                max = counter.getValue(task)
                maxCount = 1
            }
        }

        // 频率最多的任务安排之间存在 ${partCount} 个空间
        val partCount = max - 1
        // 每个空间内有 ${partLength} 个空隙
        val partLength = n - (maxCount - 1)
        // 总共有 ${emptySlot} 个空槽可以插入任务
        val emptySlots = partCount * partLength
        // 剩余 ${availableTasks} 个任务(原始任务数,除掉频率最高的一个(或几个)任务)
        val availableTasks = tasks.size - max * maxCount
        // 剩余的空位
        val idles = max(0, emptySlots - availableTasks)

        return tasks.size + idles
    }
}

参考资料:

  1. Java O(n) time O(1) space 1 pass, no sorting solution with detailed explanation
posted @ 2020-11-12 12:29  Zhongju.copy()  阅读(120)  评论(0编辑  收藏  举报