You are given an array of CPU tasks, each labeled with a letter from A to Z, and a number n. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of at least n intervals between two tasks with the same label. Return the minimum number of CPU intervals required to complete all tasks.
Example 1
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: Each A must be at least 2 slots apart, and so must each B. Interleaving them gives A B idle A B idle A B, which is 8 intervals.
Example 2
Input: tasks = ["A","A","A","B","B","B"], n = 0
Output: 6
Explanation: With n = 0 no cooldown is needed, so the 6 tasks run back to back in 6 intervals.
Example 3
Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output: 16
Explanation: The six A's need two slots between each pair: A _ _ A _ _ A _ _ A _ _ A _ _ A, which is 16 intervals. The six other tasks fill six of the ten gaps, leaving 4 idle slots, so the total is 16.
Always run the task with the most remaining copies (max-heap). After running it, it must cool down for n intervals before it can return to the heap, so park it in a timed queue.
Run checks all cases above. Submit evaluates all test cases.