19.Task Scheduler

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.

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

Solution: (Priority Queue)

Sorting according to the frequency

class Solution
{
public:
    int leastInterval(vector<char> &tasks, int n)
    {
        if (n == 0)
        {
            return tasks.size();
        }

        vector<int> arr(26, 0);
        for (int i = 0; i < tasks.size(); i++)
        {
            arr[tasks[i] - 65]++;
        }

        priority_queue<int, vector<int>> pq;

        for (int i = 0; i < 26; i++)
        {
            if (arr[i] > 0)
            {
                pq.push(arr[i]);
            }
        }

        vector<int> temp;
        int res = 0;

        while (!pq.empty())
        {

            int time = 1;

            temp.push_back(pq.top());
            pq.pop();

            for (int i = 0; i < n; i++)
            {
                if (!pq.empty())
                {
                    temp.push_back(pq.top());
                    pq.pop();
                    time++;
                }
            }

            for (int i = 0; i < temp.size(); i++)
            {
                if (temp[i] - 1 > 0)
                {
                    pq.push(temp[i] - 1);
                }
            }

            res += (pq.empty()) ? time : n + 1;
            temp.clear();
        }

        return res;
    }
};

Time Complexity: O(n log n)

Solution :

class Solution
{
public:
    int leastInterval(vector<char> &tasks, int n)
    {
        if (n == 0)
        {
            return tasks.size();
        }

        vector<int> arr(26, 0);
        for (int i = 0; i < tasks.size(); i++)
        {
            arr[tasks[i] - 65]++;
        }

        sort(arr.begin(), arr.end(), greater<int>());

        int maxEle = arr[0] - 1; //getting the max frquency
        int idle = maxEle * n; //no of idle gaps 

        for (int i = 1; i < 26; i++)
        {
            idle -= min(maxEle, arr[i]); //filling the gaps
        }

        if (idle < 0)
        {
            return tasks.size();
        }

        return idle + tasks.size();
    }
};

Time Complexity: O(n)

Last updated