Leetcode: Task Scheduler


This question is commonly asked at Facebook and is as follows: 

Given a character array tasks, representing the tasks that a CPU needs to do where each letter represents a different task. Tasks can be done in any order and each task is done in one unit of time. There is a non-negative integer representing the cooldown between 2 same tasks, which means there must at least n units of time between the same 2 tasks. Return the least number of times that the CPU will take to finish all the given tasks. Here are the test cases: 

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 1:

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.

Example 2:


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.

Example 3:


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

The approach 1 is the greedy approach and the total number of CPU intervales consists of both busy and idle slots. The number of busy slots is define by the number of tasks to execute (len(tasks)). The maximum possible idle slots is (f_max - 1) * n. We can subsequently sort all the tasks by frequency and fulfill as many idle slots as one could. 



The maximum number of tasks is 26, so there should be an array to keep the frequencies of each task. Iterate through an input array and store the frequency of task A at index 0, B at index 1, etc, and sort the array and get the maximum frequency.  Pick the elements in descending order and decrease the idle time by the current frequency and return the busy slots added with the idle slots. Deal with the pigeonhole principle in the problem. 

class Solution {

    public int leastInterval(char[] tasks, int n) {

        //get the frequencies

        int[] frequencies = new int[26];

        for(int t : tasks) {

            frequencies[t - 'A']++;

        }

        //order by the greatest frequency

        Arrays.sort(frequencies);

        int f_max = frequencies[25];

        int idle_time = (f_ma - 1) * n;

        //shuffle the words inside 

        for(int i = frequencies.length - 2; i >= 0 && idle_time > 0; --i) {

            idle_time -= Math.min(f_max - 1, frequencies[i]);

        }

        //idle time bust be either a positive or neutral number

        idle_time = Math.max(0, idle_time);

        return idle_time + tasks.length;

    }

}


Now we decrease the idle time by that is not the maximum frequency, until either there is no idle time (the array is filled out), or all the numbers are fulled, since everything can be filled relatively easily in a sequential pattern like ABCABCABC if everything is two. But what if there are more letters? Like ABCD? Then we go ABCABCABC then D    D     D    D    leaving everything out, we just ignore the rest of their letters by (f_ma) * n, where n is the length of the array. 



Comments

Popular Posts