Leetcode刷题java之621. 任务调度器

执行结果:

通过

显示详情

执行用时 :2 ms, 在所有 Java 提交中击败了100.00% 的用户

内存消耗 :37.9 MB, 在所有 Java 提交中击败了88.85%的用户

题目:

给定一个用字符数组表示的 CPU 需要执行的任务列表。其中包含使用大写的 A - Z 字母表示的26 种不同种类的任务。任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时间内执行完。CPU 在任何一个单位时间内都可以执行一个任务,或者在待命状态。

然而,两个相同种类的任务之间必须有长度为 n 的冷却时间,因此至少有连续 n 个单位时间内 CPU 在执行不同的任务,或者在待命状态。

你需要计算完成所有任务所需要的最短时间。

示例 1:

输入: tasks = ["A","A","A","B","B","B"], n = 2
输出: 8
执行顺序: A -> B -> (待命) -> A -> B -> (待命) -> A -> B.


注:


    任务的总个数为 [1, 10000]。
    n 的取值范围为 [0, 100]。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/task-scheduler
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:

参考https://leetcode-cn.com/problems/task-scheduler/solution/ren-wu-diao-du-qi-by-leetcode/

的第三种方法

代码:

class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] hash=new int[26];
        for(char c:tasks)
        {
            hash[c-'A']++;
        }
        Arrays.sort(hash);
        int max=hash[25]-1;
        int idea_time=max*n;
        for(int i=24;i>=0&&hash[i]>0;i--)
        {
            idea_time-=Math.min(max,hash[i]);
        }
        return idea_time>0?idea_time+tasks.length:tasks.length;
    }
}
发布了481 篇原创文章 · 获赞 502 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/qq_41901915/article/details/104244929