ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-11-12 12:31:37  阅读:261  来源: 互联网

标签:621 tasks max idle task 任务 Task 任务调度 CPU


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

标签:621,tasks,max,idle,task,任务,Task,任务调度,CPU
来源: https://www.cnblogs.com/zhongju/p/13963294.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有