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 one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks,
there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

Example:

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

解法一: 计算得到词频最大的字符,该字符相邻最大的数组偏移小于 n
通过 HashMap 统计词频,PriorityQueue 保证有序性

public int leastInterval(char[] tasks, int n) {
    Map<Character, Integer> freqs = new HashMap<>();
    for (int i = 0; i < tasks.length; i++)
        freqs.put(tasks[i], freqs.getOrDefault(tasks[i], 0) + 1);

    PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue<>(
            (a,b) -> a.getValue() != b.getValue() ? b.getValue() - a.getValue() : a.getKey() - b.getKey());
    pq.addAll(freqs.entrySet());

    int count = 0;
    int maxFreq = pq.peek().getValue();
    while (!pq.isEmpty() && pq.peek().getValue() == maxFreq) {
        pq.poll();
        count ++;
    }
    return Math.max(tasks.length, (maxFreq - 1) * (n + 1) + count);
}

解法二: 通过 HashMap 统计原始词频, LinkedHash 根据 Map.Entry.getValue() 进行排序

public int leastInterval(char[] tasks, int n) {
    Map<Character, Integer> freqs = new HashMap<>();
    for (int i = 0; i < tasks.length; i++)
        freqs.put(tasks[i], freqs.getOrDefault(tasks[i], 0) + 1);

    freqs = sortByValue(freqs);
    int maxFreq = freqs.entrySet().iterator().next().getValue();
    int count = 0;
    for (Map.Entry<Character, Integer> entry : freqs.entrySet()) {
        if (entry.getValue() == maxFreq)
            count ++;
        else
            break;
    }
    return Math.max(tasks.length, (maxFreq - 1) * (n + 1) + count);
}

private Map<Character, Integer> sortByValue(Map<Character, Integer> map) {
    List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
    list.sort((o1, o2) -> !o1.getValue().equals(o2.getValue()) ? Integer.compare(o2.getValue(), o1.getValue()) : Character.compare(o1.getKey(), o2.getKey()));
    Map<Character, Integer> newMap = new LinkedHashMap<>();
    for (Map.Entry<Character, Integer> entry : list)
        newMap.put(entry.getKey(), entry.getValue());
    return newMap;
}

原文地址:https://www.cnblogs.com/janh/p/10713809.html

时间: 2024-10-11 22:38:32

leetcode-651.Task Scheduler的相关文章

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

[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

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

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