mediumGreedyPure DSA~30 min
Task Scheduler With Cooldown
A worker runs CPU tasks labeled by type. Two tasks of the same type must be separated by at least n idle intervals (a cooldown). Each task and each idle slot takes one interval. Compute the least number of intervals to finish all tasks.
Problem
Given a char array tasks representing task types and an integer n (the cooldown between two same-type tasks), return the minimum number of intervals needed to execute all tasks. Each interval runs exactly one task or stays idle.
Input
An array tasks of uppercase letters and an integer n (cooldown).
Output
An integer: the minimum number of intervals.
Constraints
- 1 <= tasks.length <= 10^4
- tasks[i] is an uppercase English letter.
- 0 <= n <= 100
Examples
Example 1
Input
tasks = ["A","A","A","B","B","B"], n = 2
Output
8
A B idle A B idle A B → 8 intervals.
Example 2
Input
tasks = ["A","A","A","B","B","B"], n = 0
Output
6
No cooldown; run them back to back.