Task Scheduler
01 · Problem
You are given an array of CPU tasks, each represented by a character from 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 the CPU will take to finish all the given tasks.
02 · Examples
tasks = ["A","A","A","B","B","B"], n = 2
8
A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B. Total of 8 intervals.
tasks = ["A","C","A","B","D","B"], n = 1
6
A possible sequence is: A -> B -> C -> D -> A -> B. No idle intervals needed.
tasks = ["A","A","A","B","B","B"], n = 0
6
With n = 0, no cooling is needed. Any order works and takes 6 intervals total.
03 · Constraints
- 011 <= tasks.length <= 104
- 02tasks[i] is an uppercase English letter
- 030 <= n <= 100
04 · Optimal complexity
- Time
- O(n)
- Space
- O(1)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.