Skip to main content

Task Queue with Cooldown Scheduler

medium
QueueHeapGreedyHash TableSortingTop K HeapGreedy Interval
Asked atAmazonMicrosoftGoogleMetaUber

Problem Description

You have a list of tasks, each identified by an uppercase letter (A–Z). A CPU can execute one task per time unit. After executing a task of type `X`, the CPU must wait at least `cooldown` time units before it can execute another task of type `X`. During the wait the CPU is idle.

Given the array `taskList` of task labels and an integer `cooldown`, return the **minimum number of time units** required to finish all tasks.

**Example 1:**
```
Input: taskList = ['A','A','A','B','B','C'], cooldown = 2
Output: 7
Explanation: A -> B -> C -> A -> B -> idle -> A
```

**Example 2:**
```
Input: taskList = ['X','X','Y','Y'], cooldown = 0
Output: 4
Explanation: No cooldown needed; run in any order.
```

**Example 3:**
```
Input: taskList = ['M','M','M'], cooldown = 3
Output: 9
Explanation: M -> idle -> idle -> idle -> M -> idle -> idle -> idle -> M
```

Constraints

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

Follow-up

Instead of just the total time, return the actual scheduled sequence of tasks (and 'idle' slots) as a list of strings.

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.