Task schedule

Posted tobeabetterpig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Task schedule相关的知识,希望对你有一定的参考价值。

Task schedule 

https://leetcode.com/problems/task-scheduler/solution/

https://github.com/tongzhang1994/Facebook-Interview-Coding/blob/master/Task%20schedule%20with%20cool%20down%20time.java

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.









Approach #1 Using Sorting

public class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] map = new int[26];
        for (char c: tasks)
            map[c - ‘A‘]++;
        Arrays.sort(map);
        int time = 0;
        while (map[25] > 0) {
            int i = 0;
            while (i <= n) {
                if (map[25] == 0)
                    break;
                if (i < 26 && map[25 - i] > 0)
                    map[25 - i]--;
                time++;
                i++;
            }
            Arrays.sort(map);
        }
        return time;
    }
}












Approach #2 Using Priority-Queue 


public class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] map = new int[26];
        for (char c: tasks)
            map[c - ‘A‘]++;
        PriorityQueue < Integer > queue = new PriorityQueue < > (26, Collections.reverseOrder());
        for (int f: map) {
            if (f > 0)
                queue.add(f);
        }
        int time = 0;
        while (!queue.isEmpty()) {
            int i = 0;
            List < Integer > temp = new ArrayList < > ();
            while (i <= n) {
                if (!queue.isEmpty()) {
                    if (queue.peek() > 1)
                        temp.add(queue.poll() - 1);
                    else
                        queue.poll();
                }
                time++;
                if (queue.isEmpty() && temp.size() == 0)
                    break;
                i++;
            }
            for (int l: temp)
                queue.add(l);
        }
        return time;
    }
}









================================



顺序已经规定好了的task schedule:输出的是最后的时间

thread: 1, 2, 1, 1, 3, 4; 冷却时间: 2 time slot,scheduler应该是这样的:1, 2, _, 1, _, _, 1, 3, 4,最后返回9.


private static int taskSchedule1(int[] tasks, int cooldown) {
    if (tasks == null || tasks.length == 0)    return 0;
    HashMap<Integer, Integer> map = new HashMap<>();
    int slots = 0;
    for (int task : tasks) {
        //if we need to wait for the cooldown of the same task, just update the slots
        if (map.containsKey(task) && map.get(task) > slots) 
            slots = map.get(task);
        }
        //update the time slot to the time when curr task can be done again
        map.put(task, slots + 1 + cooldown); 
        slots++; //important!! update the used 1 slot of curr task
    }
    return slots;
}













顺序已经规定好了的task schedule:输出的是字符串 ‘_‘

//if we need to output a string of the task scheduling(without reordering), eg.1,2,1,1,3,4, k=2, -> 12_1__134

public String taskSchedule2(int[] tasks, int cooldown) {
   if (tasks == null || tasks.length == 0)   return "";
   Map<Integer, Integer> map = new HashMap<>();//store indices, where cooldown stops, of tasks in window
   int slots = 0;
   StringBuilder sb = new StringBuilder();
   for (int task : tasks) {
       if (map.containsKey(task) && map.get(task) > slots) {
           int waitingTime = map.get(task) - slots;
           while (waitingTime-- > 0) 
               sb.append("_");
           slots = map.get(task);
       }
       map.put(task, slots + cooldown + 1);
       sb.append(task);
       slots++;
   }
   return sb.toString();
}
      
      

 

以上是关于Task schedule的主要内容,如果未能解决你的问题,请参考以下文章

表 '***.tx_scheduler_task' 在“composer typo3/cms-scheduler”之后不存在

Spring3 Schedule Task之注解实现 (两次起动Schedule Task 的解决方案)

与 tbb::task_arena 和 tbb::task_scheduler_observer 链接时出错

LeetCode Task Scheduler

621. Task Scheduler

hdu 3572 Task Schedule