Task Scheduler

Heaps & Top-K, problem 5 of 7

Task Scheduler

Medium

LC #621

max-heapgreedysimulationqueue

Not attempted yet

A CPU runs tasks labelled with capital letters. Each task takes one time slot, and in each slot the CPU either runs one task or sits idle. Two runs of the same letter must be separated by at least n slots. Tasks can run in any order.

Return the minimum number of slots needed to run every task.

Example 1

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8

A B idle A B idle A B

Example 2

Input: tasks = ["A","C","A","B","D","B"], n = 1
Output: 6

A B A B C D: no idle slots needed.

Example 3

Input: tasks = ["A","A","A","B","B","B"], n = 3
Output: 10

A B idle idle A B idle idle A B

Constraints

  • 1 <= len(tasks) <= 3 * 10^4
  • tasks[i] is an uppercase letter
  • 0 <= n <= 100

Python

Loading draft…

Test results

9 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 6 hidden

Run examples, then submit all tests.