You are given an array of CPU tasks, each represented by letters A to Z, and a cooling interval n. Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there's a constraint: identical tasks must be separated by at least n intervals due to cooling time.
Return the minimum number of intervals required to complete all tasks.
Greedy Insight: The task with maximum frequency determines the minimum time. Arrange the most frequent task first with gaps of n, then fill the gaps with other tasks.
Why Greedy Works: The bottleneck is the most frequent task. We need (maxFreq - 1) * (n + 1) + countOfMaxFreqTasks intervals at minimum. However, if we have many diverse tasks, they might fill all the gaps, so the answer is max(formula_result, total_tasks).
tasks = ["A","A","A","B","B","B"], n = 28tasks = ["A","C","A","B","D","B"], n = 16tasks = ["A","A","A","B","B","B"], n = 061 <= tasks.length <= 10^4tasks[i] is an uppercase English letter0 <= n <= 100Click "Run" to execute your code against test cases
Socratic guidance - I'll ask questions, not give answers