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
Solution
The key is to find out how many idles do we need. Let’s say the most frequent tasks occur M times. Two tasks of the same type collide if they are scheduled within N time of each other. First consider the most frequent characters, we can determine their relative positions first and use them as a frame to insert the remaining less frequent characters. Let F be the set of most frequent chars with frequency M. We can create M slots, each slot is identical and is a string consists of chars in F in a specific fixed order, then we insert the less frequent chars into the gaps between these slots sequentially one by one ordered by frequency in a decreasing order and try to fill the k-1 gaps as full or evenly as possible each time you insert a character. E.g. we have following tasks : 3 A, 2 B, 1 C. and we have N = 2. We should first arrange A, and then B and C. Imagine there are “slots” and we need to arrange tasks by putting them into “slots”. Then A should be put into slot 0, 3, 6 since we need to have at least N = 2 other tasks between two A. After A put into slots, it looks like this:
Step 1: A ? ? A ? ? A "?" is empty slots. Step 2: Now we can use the same way to arrange B and C. The finished schedule should look like this: A B C A B # A "#" is idle
Problem asks for number of CPU intervals i.e. number of idles + number of tasks. In the above example A separated slots into (count(A) – 1) = 2 parts (partCount ), each part has length N. Since A is the task with most frequency, it should need more idles than any other tasks. So if we can get how many idles we need to arrange A, we will also get number of idles needed to arrange all tasks.
Example: Let the number of instances of tasks A, B, C, D, E are 6, 1, 1, 1, 1 respectively with N=2(cooling time). If we firstly give 1 round to each A, B, C, D and E. Now, only 5 instances of A are pending, but each instance will take 3 time units to complete because of cooling time. But a better way to schedule the tasks will be this: A, B, C, A, D, E, ... . In this way, by giving turn to the task A as soon as its cooling time is over, we can save a good number of clock cycles. From the above example, its cleart that tasks with maximum number of frequency will contribute to a large number of idle cycles in the future.
After arranging A, number of empty slots: emptySlots = partCount * N; we can also get how many tasks we have to put into those slots: leftTasks = tasks.length – count(A). If emptySlots > leftTasks which means we have not enough tasks available to fill all empty slots, we must fill them with idles. Thus we have
idles = max(0, emptySlots - leftTasks)