码迷,mamicode.com
首页 > 其他好文 > 详细

Task schedule

时间:2018-08-17 01:20:10      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:least   res   bre   HERE   .so   zha   sch   ++   hang   

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

标签:least   res   bre   HERE   .so   zha   sch   ++   hang   

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9490937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!