Article start
DSA Course: Interview Patterns and Problem Solving
Module 12: Heap & Priority Queue

Task Scheduler: Greedy Max-Heap Pattern

Schedule frequent tasks while respecting cooldown gaps.

May 29, 2026·25

Learning Outcome

After this lesson, you should be able to schedule the most frequent remaining tasks first and avoid overcounting idle time in the last cycle.

Problem Statement

Given tasks and cooldown n, return the least number of intervals needed to finish all tasks.

InputOutputWhy
tasks = ["A","A","A","B","B","B"], n = 28One valid schedule is A B idle A B idle A B.

Brute Force Approach

Try every task order and test cooldown validity. This grows too quickly.

Optimized Approach

Count task frequencies, use a max-heap, and process cycles of length n + 1 by taking the most frequent remaining tasks.

Exact Pseudocode

count task frequencies
heap = max heap of counts
time = 0
while heap is not empty:
  used = []
  slots = 0
  repeat up to n + 1 times while heap has tasks:
    count = pop max - 1
    slots += 1
    if count still positive:
      add count to used
  push used counts back
  if heap is empty:
    time += slots
  else:
    time += n + 1
return time

Reference Code

import heapq
from collections import Counter

class Solution:
    def leastInterval(self, tasks, n):
        heap = [-count for count in Counter(tasks).values()]
        heapq.heapify(heap)
        time = 0

        while heap:
            used = []
            slots = 0
            for _ in range(n + 1):
                if heap:
                    count = heapq.heappop(heap) + 1
                    slots += 1
                    if count < 0:
                        used.append(count)
            for count in used:
                heapq.heappush(heap, count)
            time += n + 1 if heap else slots

        return time

Sample Dry Run

StepStateResult
CountsA:3, B:3heap has 3 and 3
Cycle 1Run A, B, then idletime += 3
Cycle 2Run A, B, then idletime += 3
Last cycleRun A, Btime += 2, total 8

Complexity

MeasureValueReason
TimeO(t log u)t tasks are processed through a heap of u unique task types.
SpaceO(u)The heap and temporary used list store unique task counts.

Edge Cases

  • If n = 0, answer is the number of tasks.
  • The last cycle should not add unnecessary idle time.
  • Task order can vary as long as cooldown is valid.

Interview Checklist

  • Use max-heap counts, not raw task letters.
  • Process at most n + 1 tasks per cycle.
  • Only add idle slots when tasks remain after the cycle.

FAQs

Why choose most frequent tasks first?

They create the most cooldown pressure, so scheduling them early reduces future idle gaps.

Why not always add n + 1?

The final cycle may finish before all cooldown slots are needed.

What is the core pattern?

Greedy scheduling with a max-heap.

Test your knowledge

Take a quick quiz based on this chapter.

Discussion

0 comments

Sign in to share a question or add to the discussion.
Start the discussion

Ask a question or share what stood out to you.