LeetCode621——任务调度器

本文详细解析了LeetCode621任务调度器问题的三种解法,包括每一轮分配后重新排序、优先队列实现以及计算总的等待时间。每种方法都分析了时间复杂度和空间复杂度,并提供了JAVA代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的LeetCode代码仓:https://github.com/617076674/LeetCode

原题链接:https://leetcode-cn.com/problems/task-scheduler/

题目描述:

知识点:优先队列、贪心算法

思路一:每一轮分配后重新排序

优先分配数目最多的任务,当任务间隔i <= n时,即使没有任务分配,结果时间也要增加,这是冷却时间。

每一轮任务分配后都要重新排序。

时间复杂度是O(result),其中result为完成所有任务所需要的最短时间。空间复杂度是O(1)。

JAVA代码:

public class Solution {

    public int leastInterval(char[] tasks, int n) {
        int[] count = new int[26];
        for (int i = 0; i < tasks.length; i++) {
            count[tasks[i] - 'A']++;
        }
        Arrays.sort(count);
        int result = 0;
        while (count[25] > 0) {
            int i = 0;
            while (i <= n) {
                if (count[25] == 0) {
                    break;
                }
                if (i < 26 && count[25 - i] > 0) {
                    count[25 - i]--;
                }
                result++;
                i++;
            }
            Arrays.sort(count);
        }
        return result;
    }

}

LeetCode解题报告:

思路二:优先队列实现

用优先队列的出队和入队操作代替思路一的排序过程。

时间复杂度是O(result),其中result为完成所有任务所需要的最短时间。空间复杂度是O(1)。

JAVA代码:

public class Solution {

    public int leastInterval(char[] tasks, int n) {
        int[] count = new int[26];
        for (int i = 0; i < tasks.length; i++) {
            count[tasks[i] - 'A']++;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());
        for (int i = 0; i < 26; i++) {
            if (count[i] != 0) {
                priorityQueue.add(count[i]);
            }
        }
        int result = 0;
        while (!priorityQueue.isEmpty()) {
            int i = 0;
            List<Integer> list = new ArrayList<>();
            while (i <= n) {
                if (!priorityQueue.isEmpty()) {
                    if (priorityQueue.peek() > 1) {
                        list.add(priorityQueue.poll() - 1);
                    } else {
                        priorityQueue.poll();
                    }
                }
                result++;
                if (priorityQueue.isEmpty() && list.size() == 0) {
                    break;
                }
                i++;
            }
            for (Integer integer : list) {
                priorityQueue.add(integer);
            }
        }
        return result;
    }

}

LeetCode解题报告:

思路三:计算总的等待时间

假设最大任务执行次数是p,该任务编号是A,我们不考虑其他任务,其需要的等待时间是(p - 1) * n。

(1)如果还有执行次数为p的最大任务,该任务编号是B,该任务中的(p - 1)个任务可以在A的等待时间内完成,如果B在A的等待时间内完成,那么那段时间就不能算进等待时间里,因此总等待时间需要减去p - 1。

(2)如果某个执行次数为q的任务,其任务编号是C,显然,所有该任务都能再原来的等待时间内完成,因此总等待时间需要减去q。

最后返回结果时,如果计算出的等待时间是负数,这是不切实际的,我们直接返回tasks数组的长度。否则,我们返回的是tasks数组的长度加上等待时间。

时间复杂度是O(m),其中m为任务数组的长度。空间复杂度是O(1)。

JAVA代码:

public class Solution {

    public int leastInterval(char[] tasks, int n) {
        int[] count = new int[26];
        for (int i = 0; i < tasks.length; i++) {
            count[tasks[i] - 'A']++;
        }
        Arrays.sort(count);
        int maxVal = count[25] - 1, idleSlots = maxVal * n;
        for (int i = 24; i >= 0 && count[i] > 0 ; i--) {
            idleSlots -= Math.min(count[i], maxVal);
        }
        if (idleSlots > 0) {
            return idleSlots + tasks.length;
        } else {
            return tasks.length;
        }
    }

}

LeetCode解题报告:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值