Task Scheduler

You are given a list of tasks represented by capital letters and a non-negative cooling interval n. Each task takes one unit of time. Return the least number of time units needed to finish all tasks if identical tasks must be separated by at least n units. Pattern focus: Greedy Scheduling. Count task frequencies, schedule the most frequent task first, and fill idle slots with other tasks whenever possible.

Input Format

tasks = task list, n = cooldown interval

Output Format

minimum total intervals needed to finish all tasks

Constraints

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

Examples

Example 1:

Input:

tasks = ["A","A","A","B","B","B"]
n = 2

Output:

8

Explanation:

An optimal schedule is A B idle A B idle A B.

Example 2:

Input:

tasks = ["A","A","A","B","B","B"]
n = 0

Output:

6

Explanation:

With no cooldown, tasks can run back-to-back.

Example 3:

Input:

tasks = ["A","A","A","B","C","D"]
n = 2

Output:

7

Explanation:

A feasible schedule fits the cooldown with only one idle slot.

Loading...
Task Scheduler - Greedy DSA Problem