Similar Problems

Similar Problems not available

Task Scheduler - Leetcode Solution

LeetCode:  Task Scheduler Leetcode Solution

Difficulty: Medium

Topics: greedy hash-table heap-priority-queue array sorting  

Problem Statement:

You are given a list of tasks that need to be executed, each with a fixed start time and duration. Given a non-negative integer n that represents the cooldown period between two tasks of the same type, return the minimum number of CPU intervals needed to execute 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

Approach:

To solve this problem, we can use a greedy approach. First, we need to count the frequency of each task. Then, we sort the tasks in decreasing order of their frequency. We also maintain a list of cooldowns for each task. Initially, all the cooldowns are 0.

We start processing the tasks in the order of their frequency. For each task, we find the task with the maximum cooldown. If the maximum cooldown is greater than 0, we wait for that many intervals before processing the current task. If the maximum cooldown is 0, we can process the current task immediately.

After processing the current task, we update the cooldown of all the tasks. The cooldown of the current task is set to n+1 (n is the given cooldown period). This is because we need to leave a gap of n intervals before processing the same task again. The cooldown of all the other tasks is reduced by 1.

We repeat this process until all the tasks are processed.

Code:

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        unordered_map<char, int> freq;
        for (char t : tasks) {
            freq[t]++;
        }
        auto cmp = [&](char a, char b) {
            return freq[a] < freq[b];
        };
        priority_queue<char, vector<char>, decltype(cmp)> pq(cmp);
        for (auto p : freq) {
            pq.push(p.first);
        }
        vector<int> cooldown(26, 0);
        int intervals = 0;
        while (!pq.empty()) {
            vector<char> available;
            while (!pq.empty()) {
                char t = pq.top();
                pq.pop();
                if (cooldown[t-'A'] == 0) {
                    available.push_back(t);
                    break;
                }
            }
            if (available.empty()) {
                intervals++;
            } else {
                char t = available.front();
                available.erase(available.begin());
                freq[t]--;
                cooldown[t-'A'] = n+1;
                for (char c : available) {
                    cooldown[c-'A']--;
                }
                if (freq[t] > 0) {
                    pq.push(t);
                }
                intervals++;
            }
            for (int i = 0; i < 26; i++) {
                if (cooldown[i] > 0) {
                    cooldown[i]--;
                }
            }
        }
        return intervals;
    }
};

Time Complexity:

The time complexity of this solution is O(nlogn), where n is the number of tasks. The sorting of tasks takes O(nlogn) time, and for each task, we can spend at most O(logn) time in the priority queue. Since we process each task exactly once, the total time complexity is O(nlogn).

Task Scheduler Solution Code

1