 All Problems
Task Scheduler
medium
heap
greedy
arrays
facebook
google
amazon

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 completing one task. Tasks can be completed in any order, but there is a cooldown interval of n intervals between two identical tasks.

Return the minimum number of CPU intervals required to complete all tasks.

Example 1:

Input:
A A A B B B
2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B

Example 2:

Input:
A A A A B B
2
Output: 10
Explanation: A -> B -> idle -> A -> B -> idle -> A -> idle -> idle -> A

Constraints:

  • 1 ≤ tasks.length ≤ 10⁴
  • tasks[i] is an uppercase English letter.
  • 0 ≤ n ≤ 100

Input format: First line: space-separated task letters. Second line: cooldown n.

Output format: A single integer.

Run to check your code against the sample cases, or submit to run every case