Task scheduler

LeetCode Portal 621

The title

You are given a list of tasks that the CPU needs to perform represented by the character array Tasks. Each letter represents a different kind of task. Tasks can be executed in any order, and each task can be completed in one unit of time. At any given unit of time, the CPU can complete a task or be on standby.

However, two tasks of the same kind must have a cooling time of integer length n between them, so that there are at least n consecutive units of time for which the CPU is performing different tasks or is on standby.

You need to calculate the minimum time required to complete all the tasks.

Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.

However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.

Return the least number of units of times that the CPU will take to finish all the given tasks.

Exampe:

Input: tasks = ["A"."A"."A"."B"."B"."B"], n = 2
Output: 8
Explanation: 
A -> B -> idle -> A -> B -> idle -> A -> B
There is at least 2 units of time between any two same tasks.

Input: tasks = ["A"."A"."A"."B"."B"."B"], n = 0
Output: 6
Explanation: On this case any permutation of size 6 would work since n = 0.
["A"."A"."A"."B"."B"."B"]
["A"."B"."A"."B"."A"."B"]
["B"."B"."B"."A"."A"."A"]... And so on.Input: tasks = ["A"."A"."A"."A"."A"."A"."B"."C"."D"."E"."F"."G"], n = 2
Output: 16
Explanation: 
One possible solution is
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A

Copy the code

Constraints:

  • 1 <= task.length <= 10^4
  • tasks[i] is upper-case English letter.
  • The integer n is in the range [0, 100].

Thinking line


Their thinking

So the first thing we’re going to do is figure out how to sort things so that we have the shortest amount of time in the world.

TasksCounts, if the type is greater than N, then the one with the highest number of counts… And so on. We fill it up with calls until the tasksCouns are empty, and we calculate the minimum time.

So now the question is, how do we determine the order of the sort?

In terms of data structure, we use a two-dimensional array to store the cooling time and times of each task. Each task in tasksCounts is represented by an array [Task, nextRunTime, counts]

  • taskRepresents the task name
  • nextRunTimeIndicates the minimum next execution time of the task
  • countsRepresents how many tasks remain to be performed

Finally, we use times to simulate the passage of time and set the initial value to 0, as is the default value for all NexTrunTimes.

Finally, we have another optimization point. We can record the minimum execution time of the next time in each calculation. Finally, if the minRunTime is greater than times, we need to wait to fill it. Since each increment of times requires a large amount of time, we can optimize it by making times = minRunTime to speed up the elapsed time and reduce unnecessary code execution whenever possible.

According to the above ideas, we design the following code

/ * * *@param {character[]} tasks
 * @param {number} n
 * @return {number}* /
var leastInterval = function(tasks, n) {
    // Calculate the total number of species, if the species is greater than n, then the most first m times more, more times,... N, M... m.
    // Until the number of times is not enough, fill with standby. Or you can fill it all in and figure out how long it takes
    [task, nextRunTime, counts]
    // Set the initial time to 0 and nextRunTime to 0;
    let times = 0;
    let minRunTime = Number.MAX_SAFE_INTEGER;
    const tasksCounts = [];
    tasks.forEach( task= > {
        const i = tasksCounts.findIndex( item= > task === item[0]);
        if(i === -1) {
            tasksCounts.push([task, 0.1])}else {
            tasksCounts[i][2] + +; }});while(tasksCounts.length) {
        // Count the counts with nextRunTime <= tims and count the counts with nextRunTime <= tims
        let run;
        let runIndex;
        tasksCounts.forEach( (item, i) = > {
            if(item[1] <=times && (! run || run[2] < item[2])) {
                run = item;
                runIndex = i;
            }
            if(minRunTime > item[1]) minRunTime = item[1]})if(run) {
            run[1] += n +1;
            if(minRunTime > run[1]) minRunTime = run[1]
            if(--run[2] < =0) {
                tasksCounts.splice(runIndex, 1);
            }
        }
        times ++;
        if(minRunTime >= times) { times = minRunTime; }}return times;
};
Copy the code

This is my solution to this question. If you have any questions or better solutions, please leave a comment and interact.