Search⌘ K
AI Features

Problem: Task Scheduler

Understand how to implement a Task Scheduler using a max heap and cooldown queue to manage CPU tasks with cooldown periods. Explore a greedy approach that schedules the most frequent tasks first to minimize idle time and total CPU intervals required.

Statement

You are given an array, tasks, representing CPU tasks, where each task is labeled with an uppercase English letter from A to Z. You are also given a non-negative integer n representing the cooldown period between two identical tasks.

In each CPU interval, you may either complete exactly one task or remain idle. Tasks may be completed in any order; however, any two tasks with the same label must be separated by at least n intervals.

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

Note: The cooldown constraint applies only between tasks sharing the same label. Different tasks can be scheduled in consecutive intervals without restriction.

Constraints:

  • 11 \leq tasks.length 104\leq 10^4

  • tasks[i] is an uppercase English letter.

  • 00 \leq n 100\leq 100 ...