Algorithm
Problem Name: 621. 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.
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:
1 <= task.length <= 104
tasks[i]
is upper-case English letter.- The integer
n
is in the range[0, 100]
.
Code Examples
#1 Code Example with C Programming
Code -
C Programming
int cmp(const void *a, const void *b) {
return *(int *)a < *(int *)b ? 1 :
*(int *)a > *(int *)b ? -1 : 0;
}
int leastInterval(char* tasks, int tasksSize, int n) {
int c[26] = { 0 };
int i, k;
for (i = 0; i < tasksSize; i ++) {
c[tasks[i] - 'A'] ++;
}
qsort(c, 26, sizeof(int), cmp);
i = 1;
while (i < 26 && c[i] == c[0]) {
i ++;
}
k = (c[0] - 1) * (n + 1) + i;
return tasksSize > k ? tasksSize : k;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
unordered_mapm;
for(auto x: tasks) m[x]++;
priority_queue < pair<int, char>>pq;
for(auto x: m) pq.push(make_pair(x.second, x.first));
int sum = 0;
while(!pq.empty()){
int time = 0;
vector < pair<int, char>>v;
for(int i = 0; i < n + 1; i++)
if(!pq.empty()){
v.push_back(pq.top());
pq.pop();
time++;
}
for(auto x: v) if(--x.first) pq.push(x);
sum += !pq.empty() ? n + 1 : time;
}
return sum;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] frequencies = new int[26];
for (char task : tasks) {
frequencies[task - 'A']++;
}
Arrays.sort(frequencies);
int maxFrequency = frequencies[25];
int idleTime = (maxFrequency - 1) * n;
for (int i = frequencies.length - 2; i >= 0 && idleTime > 0; i--) {
idleTime -= Math.min(maxFrequency - 1, frequencies[i]);
}
idleTime = Math.max(0, idleTime);
return idleTime + tasks.length;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const leastInterval = function (tasks, n) {
const len = tasks.length
const cnt = Array(26).fill(0)
const A = 'A'.charCodeAt(0)
let maxFreq = 0,
maxFreqCnt = 0
for (const ch of tasks) {
const idx = ch.charCodeAt(0) - A
cnt[idx]++
if (maxFreq === cnt[idx]) {
maxFreqCnt++
} else if (maxFreq < cnt[idx]) {
maxFreqCnt = 1
maxFreq = cnt[idx]
}
}
const slot = maxFreq - 1
const numOfPerSlot = n - (maxFreqCnt - 1)
const available = len - maxFreq * maxFreqCnt
const idles = Math.max(0, slot * numOfPerSlot - available)
return len + idles
}
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Python Programming
Code -
Python Programming
class Solution:
def leastInterval(self, tasks, n):
cnt = sorted(collections.Counter(tasks).values())
idles = (cnt[-1] - 1) * n
for i in range(len(cnt) - 1): idles -= min(cnt[i], cnt[-1] - 1)
return idles > 0 and idles + len(tasks) or len(tasks)
Copy The Code &
Try With Live Editor
Input
Output
#6 Code Example with C# Programming
Code -
C# Programming
using System;
namespace LeetCode
{
public class _0621_TaskScheduler
{
public int LeastInterval(char[] tasks, int n)
{
var count = new int[26];
foreach (var ch in tasks)
count[ch - 'A']++;
Array.Sort(count);
var max = count[25];
var left_count = max - 1;
var idleTime = left_count * (n + 1);
for (int i = 25; i >= 0 && count[i] > 0 && idleTime > 0; i--)
idleTime -= Math.Min(left_count, count[i]);
if (idleTime > 0) return idleTime + tasks.Length;
else return tasks.Length;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output