Practice Problem

Task Scheduler

Difficulty: Medium

Determine the minimum number of intervals needed to execute all tasks given a cooldown period between identical tasks.

Task Scheduler

You are given an array of CPU tasks, each represented by a letter from A to Z, and a non-negative integer n that represents the cooldown period between two same tasks. Each task takes one unit of time and the CPU can either perform a task or stay idle.

Return the minimum number of intervals the CPU will take to finish all the given tasks.

Examples

Example 1:

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B

Example 2:

Input: tasks = ["A","A","A","B","B","B"], n = 0
Output: 6
Explanation: No cooldown needed, all tasks run back-to-back.

Example 3:

Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output: 16
Explanation: A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A

Constraints

  • 1 <= tasks.length <= 10^4
  • tasks[i] is an uppercase English letter
  • 0 <= n <= 100

Expected Complexity

  • Time: O(N) where N is the number of tasks
  • Space: O(1) since we have at most 26 different tasks
MEDIUM
Heap
Max Heap
Priority Queue
Greedy
Frequency Count
Intermediate

0 views

Solution

Hints

Hint 1
Hint 2
Premium
Hint 3
Premium