LeetCode 621: Task Scheduler

class Solution {
    class Task {
        public char task;
        public int numTasks;
    }
    public int leastInterval(char[] tasks, int n) {
        if (tasks.length == 0) {
            return 0;
        }

        Map<Character, Task> map = new HashMap<>();
        for (char c : tasks) {
            if (!map.containsKey(c)) {
                Task t = new Task();
                t.task = c;
                t.numTasks = 0;
                map.put(c, t);
            }
            map.get(c).numTasks++;
        }
        Queue<Task> queue = new PriorityQueue<>((t1, t2) -> t2.numTasks - t1.numTasks);
        queue.addAll(map.values());
        int result = 0;
        while (!queue.isEmpty()) {
            int k = n + 1;
            List<Task> taskList = new ArrayList<>();
            while (k > 0 && !queue.isEmpty()) {
                Task current = queue.poll();
                current.numTasks--;
                taskList.add(current);
                result++;
                k--;
            }

            for (Task task : taskList) {
                if (task.numTasks > 0) {
                    queue.offer(task);
                }
            }
            if (queue.isEmpty()) break;
            result += k;
        }
        return result;
    }
}
时间: 2024-11-08 21:58:01

LeetCode 621: Task Scheduler的相关文章

[leetcode]621. Task Scheduler任务调度

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU cou

621. Task Scheduler &amp;&amp; Rearrange String k Distance Apart

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU cou

621. Task Scheduler

Problem statement Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each

leetcode-651.Task Scheduler

621. Task Scheduler(Medium) CPU 任务调度 Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in on

Hide C# winform App Window When Started by Task Scheduler

To make a Scheduled Task run in the background, change the User running the task to "SYSTEM", and nothing will appear on your screen. Hide C# winform App Window When Started by Task Scheduler,布布扣,bubuko.com

Task Scheduler

https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx#BKMK_winui If Task Scheduler is not open, start Task Scheduler. For more information, see Start Task Scheduler. Find and click the task folder in the console tree that you want to cre

Windows Task Scheduler Fails With Error Code 2147943785

Problem: Windows Task Scheduler Fails With Error Code 2147943785 Solution: This is usually due to a permissions issue. It’s due to the user that is running the scheduled task not having the Log On As Batch Job assignment. To fix it, have your Network

使用C#创建计划任务(How to create a Task Scheduler use C# )

需求:在不定时间段运行多个后台程序(winfrom,wpf,console,等等)用于更新数据. 问题:为什么要使用计划任务,而不直接在程序中使用一个计时器来出发呢? 答:最明显的一点,使用计时器程序一直在后台运行着,但需求中只需要一天运行一次,或一个月运行一次.一直后台跑着计时这不白浪费CPU资源么. 解决方案: 1.使用windows自带的计划任务 在控制面板中可以看到,手动新建计划任务. 2.使用微软自带的类库TaskScheduler("c:\windows\system32\taska

关于无法启动Task Scheduler 服务的通用解决方案

[方法]: Ctrl+R->cmd->然后选中cmd ,点击右键,以管理员权限运行: 输入:netsh winsock reset 重启电脑后,cmd - services.msc 进去查看Task Scheduler 服务,已经开启来了 原文地址:http://blog.51cto.com/2258868/2124883