Skip to the workspace
Pebellslearn by building
Greedy, Backtracking & Dynamic Programming  ›  Lesson

Greedy Algorithms & the Night Scheduler

Greedy Algorithms 35 minTake the best-looking step, and prove it's safe

dsa / Greedy Algorithms & the Night Scheduler

Explore each scene at your own pace.

01 · Booking Friday's main stage

Booking Friday's main stage

Greedy Algorithms & the Night Scheduler

Definition
A greedy algorithm builds a solution one step at a time by always taking the choice that looks best at that moment and never revisiting it; it is correct only when some best solution always begins with that greedy choice.
In plain words
It is like packing a bag by always grabbing the next thing that seems most useful and never unpacking. That is quick, and it gives the best result only for problems where the tempting choice never rules out the best overall answer.

In short: a greedy algorithm builds its answer one piece at a time, always taking whatever looks best right now, and never revisiting that choice. That makes greedy code short — usually a sort plus one loop — but it is only correct when the problem has the greedy-choice property: some best answer really does begin with the greedy choice. Prove that with an exchange argument, or test it against brute force on small cases. Get the rule wrong and greedy still runs, it just quietly returns a worse answer.

A timeline from 18:00 to 24:00 with Nova, Luma and Zed booked in green and Kade and Rex crossed out in grey, and a dashed arrow showing the free_at pointer walking forward from 20 to 22 to 24

Booking Friday's main stage

Tunebox Live has one stage and a pile of DJs who want it. Each sent a set with a start and an end time, two sets can't share the stage, and the booker wants as many sets as possible. Sort by finish time, keep free_at (when the stage is next free), and book anything that starts at or after it:

python
watch it move · grid

Earliest finish first: each booking leaves as much of the night free as possible for the sets still to come.

Nova finishes first, at 20, so she is booked and the stage is free again from 20. 5 by 6 table. At row 0, column 0 — value ■. 10 of 30 cells filled.

1 / 5

The mistake: "best-looking" is doing a lot of work in that sentence. Earliest start sounds just as fair — first come, first served — and it fails badly:

python

Shortest-first fails too: three sets [18–21], [20–22], [21–24] — the short middle one clashes with both neighbours, so booking it first blocks the two long sets that would otherwise fit together. Only earliest finish is safe. Take any best schedule and look at its first set. The set that finishes earliest overall, E, ends no later than that first set does — so swap them in. Nothing after it can clash, you still have the same number of sets, and now the schedule starts with the greedy choice. Repeat the argument for the rest of the night and greedy keeps pace with the optimum all the way through. That's the exchange argument: show any optimal answer can be rewritten to start with the greedy choice without getting worse. Write that paragraph and the greedy is proven; if you can't, what you have is a hunch.

step through it
1sets = [["Kade", 19, 21], ["Nova", 18, 20], ["Luma", 20, 22]]
2booked, free_at = [], 0
3for name, start, end in sorted(sets, key=lambda s: s[2]):
4 if start >= free_at:
5 booked.append(name)
6 free_at = end
7print(booked)

A back-to-back set — one that starts exactly when the previous one ends — doesn't share a single minute of stage time, and the comparison has to allow that:

broken — fix it

All three back-to-back sets should be booked, so this should print 3. It prints 2.

Coming from Java/JS: the sort is the whole trick — sets.sort((a, b) => a[2] - b[2]) in JS, Arrays.sort(sets, Comparator.comparingInt(s -> s[2])) in Java. Python's key= computes one key per item instead of comparing pairs. All three sorts are stable: equal keys keep their input order, and the task relies on that.

Scene 1 of 7
Project context and further resources
You're building a piece ofTunebox — festival planner
This piece — schedule_sets(): Fits the most DJ sets into one night.
Scenario Tunebox Live has one stage on Friday and more DJ sets than hours. The night scheduler books the most sets that fit, so the most DJs get to play.
schedule_sets.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
schedule_sets(sets) → listThe names of the most non-overlapping sets, in play order.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.