Applied AI Engineer (LLM) coding learning path Level 3 This level · 11 of 27

Task Scheduler

Medium Heap Greedy Counting

Problem

You are given tasks, each a capital letter, and a cool-down n. The same task must be separated by at least n other units of time, which can be idle or a different task. Return the least number of units of time needed to finish every task.

Original: LeetCode 621: Task Scheduler.

Examples

Example 1

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

Example 2

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

Example 3

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

Constraints

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

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 621: Task Schedulerleetcode.com
  2. Java SE 17 API: Math.maxdocs.oracle.com
Previous
Next