Task Scheduler
mediumHeap / Priority Queue+20 XPTime limit 2s
A CPU must run a list of tasks, each labelled with a capital letter; every task takes one time unit. Two runs of the same task must be at least n units apart — in between, the CPU runs other tasks or stays idle. What is the minimum total time to finish all tasks?
Input:
- line 1:
L n— the number of tasks and the cooldown - line 2: the
Ltask letters (no spaces)
Output: the minimum number of time units.
Constraints: 1 ≤ L ≤ 10⁵, 0 ≤ n ≤ 100.
Example: AAABBB with n = 2 → A B idle A B idle A B → 8.
Think about it: the most frequent task decides the frame: (maxCount − 1) blocks of length n + 1, plus one slot for every task that reaches maxCount. If there are more tasks than that frame, there is no idling at all. A greedy simulation with a max-heap also works.
Sample input 1
6 2 AAABBB
Sample output 1
8
Sample input 2
9 2 ACABDCABB
Sample output 2
9
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).