Skip to content

Latest commit

 

History

History
 
 

621. Task Scheduler

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

You are given a char array representing tasks CPU need to do. It contains capital letters A to Z where each letter represents a different task. Tasks could be done without the original order of the array. 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.

You need to 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

 

Constraints:

  • The number of tasks is in the range [1, 10000].
  • The integer n is in the range [0, 100].

Related Topics:
Array, Greedy, Queue

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/task-scheduler/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    int leastInterval(vector<char>& A, int n) {
        int cnt[26] = {}, time[26] = {}, ans = 0;
        for (int n : A) cnt[n - 'A']++;
        for (int i = 0; i < A.size(); ++i) {
            int k = -1;
            ++ans;
            for (int j = 0; j < 26; ++j) {
                if (cnt[j] == 0) continue;
                if (time[j] < ans) time[j] = ans;
                if (k == -1 || time[j] < time[k] || (time[j] == time[k] && cnt[j] > cnt[k])) k = j;
            }
            ans = time[k];
            --cnt[k];
            time[k] += n + 1;
        }
        return ans;
    }
};

Solution 2.

// OJ: https://leetcode.com/problems/task-scheduler/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
// Ref: https://leetcode.com/problems/task-scheduler/discuss/104500/Java-O(n)-time-O(1)-space-1-pass-no-sorting-solution-with-detailed-explanation
class Solution {
public:
    int leastInterval(vector<char>& A, int n) {
        int cnt[26] = {}, maxCnt = 0, num = 0;
        for (int n : A) maxCnt = max(maxCnt, ++cnt[n - 'A']);
        for (int n : cnt) num += n == maxCnt;
        int partCnt = maxCnt - 1;
        int slots = (n - num + 1) * partCnt;
        int idleCnt = max(0, slots - ((int)A.size() - maxCnt * num));
        return A.size() + idleCnt;
    }
};