Practice: Task Scheduler

Problem: https://leetcode.com/problems/task-scheduler/

Recognition reminder: you are given a list of tasks (each a single uppercase letter) and a cooldown n: the same task cannot run again until at least n other slots (tasks or idles) have passed. You want the minimum number of slots to finish all tasks. “Always run the most frequent remaining task next” is a greedy move, and a heap is the natural way to keep pulling the highest-count task, so this is a greedy-with-a-heap shape.

Before you start (the five-beat rhythm)

  1. Your Pattern Card for this week is already written.
  2. Name the pattern aloud and write your approach as a plain-English comment before any code.
  3. Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
  4. If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
  5. Debrief in your commit message before moving to the next problem.

Your target

Fill these in yourself before you look at anyone else’s solution:

Target time complexity:  ____
Target space complexity: ____

Start by counting how often each task appears (a Counter). The greedy idea: in each round of n + 1 slots, run the most frequent still-remaining tasks first, because the task with the highest count is the one most at risk of forcing idle time later. A max-heap of counts lets you pull the highest-count tasks each round, decrement them, and put back the ones that still have work left. Once you have built that and watched it run, look harder at the structure: the answer is driven almost entirely by the most frequent task and how many tasks tie it for that maximum. There is a closed-form O(n) formula hiding in that observation; find it and state both approaches in your debrief.

The function you implement

Write a function least_interval(tasks: list[str], n: int) -> int in a file named solution.py in this folder (your own work repo, not the course repo). tasks is a list of single-character strings (each an uppercase letter A through Z), and n is the integer cooldown. Return the minimum number of slots needed to run every task, where each slot either runs one task or sits idle. Note that n can be 0 (no cooldown), in which case the answer is simply the number of tasks.

Where your code goes

Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.

Debrief (paste into your commit message)

1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?