Task Scheduler LeetCode

Many LeetCode problems involve scheduling, focusing on optimizing resource use and minimizing completion time, whether it's for job scheduling, CPU scheduling, or project durations. The goal is to efficiently utilize resources to enhance productivity and reduce costs across various applications. The Task Scheduler problem introduces an added complexity: a cooling period between identical tasks. This constraint requires careful planning to respect cooldown periods while still optimizing resource use and minimizing overall completion time, reflecting the real-world challenges faced in task management systems.

Problem statement

Given a character array tasks, where each character represents a unique task and a positive integer n that represents the cooling period between any two identical tasks, find the minimum number of time units the CPU will need to complete all the given tasks. Each task requires one unit to perform, and the CPU must wait for at least n units of time before it can repeat the same task. During the cooling period, the CPU can perform other tasks or remain idle.

Constraints:

  • 1 1 \leq  tasks.length 1000 \leq 1000

  • 00 \leq n 100 \leq 100

  • tasks consists of uppercase English letters

Let’s take a look at a few examples to get a better understanding of the problem statement:

canvasAnimation-image
1 of 3

Quick quiz

Let’s take a moment to ensure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem.

Task Scheduler

1

What’s the minimum number of units of time if the following values are given as input?

Tasks = [A, A, A, B, B, C, C]

n = 1

A)

3

B)

7

C)

8

D)

9

Question 1 of 30 attempted

Solution

An optimal strategy is to schedule the most frequent task first, then the second most frequent task, and so on. This is because once we’ve scheduled the most frequent task, we can get an estimate of the maximum idle time. Therefore, we keep track of the frequencies of all tasks using a hash map, frequencies. Let’s say we have tasks = [A,B,C,A,B,A][A,B,C,A,B,A] and n = 2, then frequencies = {A:3, B:2, C:1}\{ A: 3, ~B: 2, ~C:1\}. Then, we sort these frequencies so that we can use them later to schedule the tasks in descending order. The most frequent task, i.e., AA can be scheduled as A _ _ A _ _ AA ~\_~\_~A~\_~\_~A. Here, we have four idle slots, which can be calculated as:

              Idle time=(Max frequency1)×nIdle~time = (Max~frequency - 1) \times n

Next, we utilize these idle slots to schedule the remaining tasks. The next most frequent task is BB, which can be scheduled as: A B _ A B _ AA ~B~\_~A~B~\_~A. Now, we’re left with only two idle slots. In general, for every scheduled task, other than the most frequent, the idle slots can be calculated as:

            Idle time=Idle timeCurrent frequencyIdle~time = Idle~time - Current~frequency

Finally, we can schedule task CC in any of the two idle slots. For example: A B C A B _ AA ~B~C~A~B~\_~A. After scheduling task CC, all tasks have been scheduled. Therefore, the CPU requires 77 units of time to perform tasks = [A,B,C,A,B,A][A,B,C,A,B,A] with n = 2.

The formula above works fine as long as Current frequency<Max frequencyCurrent~frequency < Max~frequency. In other words, if we have more than one task having the maximum frequency, the equation above doesn’t hold. Therefore, we consider the maximum frequency at every step, and the formula is modified as:

      Idle time=Idle timemin(Max frequency1, Current frequency)Idle~time = Idle~time - min(Max~frequency - 1,~ Current~frequency)

For example, tasks = [A,A,B,B][A,A,B,B] and n = 22. After scheduling task AA as A _ _ AA~\_~\_~A , the remaining idle slots become 22. Now, if we schedule task BB as A B B AA~B~B~A, we break the cooling period rule. Therefore, we need to schedule it as A B _ A BA~B~\_~A~B. Using the equation above, the idle time comes out to be 11 because Idle time=2min(21, 2)=1Idle~time = 2 - min(2 - 1,~ 2) = 1.

In general, the total time required by the CPU can be calculated as:

            Total time=Number of tasks+Idle timeTotal~time = Number~of~tasks + Idle~time

Let’s look at the following illustration to get a better understanding of the solution:

canvasAnimation-image
1 of 8

Let’s look at the code for the algorithm we just discussed.

Complexity analysis

Let’s analyze the time and space complexity of our solution.

Time complexity

The time complexity of counting the frequencies of tasks is O(n)O(n), where nn is the total number of tasks. Sorting the frequencies takes constant time O(1)O(1) due to the fixed size (26 for the 26 letters of the alphabet). The proceeding steps also take constant time. Consequently, the overall time complexity of the solution is O(n)O(n).

Space complexity

The space complexity is O(1)O(1) because it requires constant space to store task frequencies.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved