Oven Cooldown Planner

Medium · Heaps & Top-K · about 30 min

A bakery has one oven and a list of baking jobs, each labelled with a capital letter for the recipe ("A", "B", ...). Every time unit, the oven either bakes one job or stays idle. After baking a recipe, the oven must wait at least gap time units before it bakes the same recipe again (it may bake other recipes or stay idle in between).

Jobs can be done in any order. Return the smallest number of time units needed to finish every job.

  • gap >= 0. jobs may be empty.

Example: cooldown_schedule(["A", "A", "A", "B", "B", "B"], 2) → 8 (A B idle A B idle A B)

Hints

  • Only how many jobs each recipe has matters, not the letters themselves.
  • At each time unit, bake the recipe with the most jobs left that is not cooling down. A max-heap gives you that recipe.
  • After baking, a recipe waits in a queue with the time it becomes free again. Move it back into the heap when that time comes.

Learn the pattern first: the Heaps & Top-K lesson.

More Heaps & Top-K problems