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 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 1:

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

Note:

  1. The number of tasks is in the range [1, 10000].
  2. The integer n is in the range [0, 100].

Solution

This is also a pure math problem. Generally, it depends on the maximum number of tasks.

Basic idea is divided into three categories:

  • Find the most appeared task, employ a hash table, indexed by the tasks.
  • Count the number of most appeared task, calculate the return value.
  • If the return value is less than the size of array(because the cooling time is too short). return the size of array.

Time complexity is O(n), space complexity is O(n).

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        unordered_map<char, int> hash_table;
        int max_val = 0;
        for(auto c : tasks){
            hash_table[c]++;
            max_val = max(max_val, hash_table[c]);
        }
        int max_cnt = 0;
        for(auto it : hash_table){
            if(it.second == max_val){
                max_cnt++;
            }
        }
        int task_size = tasks.size();
        return max(task_size, (n + 1) * (max_val - 1) + max_cnt);
    }
};
时间: 2024-11-20 09:19:48

621. Task Scheduler的相关文章

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

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

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