Tuesday, February 23, 2021

[Google Question][LeetCode] Task Scheduler

Problem: 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.

Example:

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


Approach: There are two possible situations:

1. When the most frequent task is not frequent enough to force idle slots (#):

| A | B | C | D | A | with n = 2 => | A | B | C | A | D |  

2. When the most frequent task is frequent enouth to force idle slots (#):

| A | B | C | D | A |  A | with n = 2 => | A | B | C | A | D | # | A

In situation 1, the solution is straight forward, it is just the number of tasks, but in situation 2 we need to dig in more. Say the maximum frequency is FreqMax and number of taks with maximum frequency is NumTasksWithMaxFreq. Now look at the following example:

| A | B | C | D | B | A | B | A | A | B | 

Here FreqMax is 4 and NumTasksWithMaxFreq = 2

Output is:

| A | B | C | A | B | D | A | B | # | A | B |

Now if you see we need n + 1 slots (1 for execution and n idle slots) for FreqMax - 1 (last time we don't need idle slots)times. At the end we need to execute NumTasksWithMaxFreq 1 remaining time so the number of slots required are:

(n + 1) * (FreqMax - 1) + NumTasksWithMaxFreq 

Obviously max of situation 1 result and situation 2 result is our answer.


Implementation in C#:

    public int LeastInterval(char[] tasks, int n) 

    {        

        if (n == 0)

        {

            return tasks.Length;

        }

        int[] tasksFrequencies = new int[26];

        int maxFrequency = 0;

        foreach (char task in tasks)

        {

            ++tasksFrequencies[task - 'A'];

            if (tasksFrequencies[task - 'A'] > maxFrequency)

            {

                maxFrequency = tasksFrequencies[task - 'A'];

            }

        }

        int tasksWithMaxFrequency = 0;

        foreach (int frequency in tasksFrequencies)

        {

            if (frequency == maxFrequency)

            {

                ++tasksWithMaxFrequency;

            }

        }

        return Math.Max(tasks.Length, (maxFrequency - 1) * (n + 1) + tasksWithMaxFrequency);

    }


Complexity: O(n)

No comments:

Post a Comment