Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 08 · DSA · Worked problems, in English · about 17 min to read

Play it as a journeyTutor comes with a plan

Clinic #29: Non-overlapping Intervals

Algorithm Design Patterns
Algorithm Design Patterns · Open board

A catalogue of ways to attack a problem. Its greedy panel says the rule out loud: take the locally best choice, never look back, and prove the choice safe. This clinic is that proof, done once by hand on a real problem.

Before you start. Clinic #28: Merge Intervals took a pile of road closures, sorted them by where they start, and walked the road once, widening one sign at a time. If you have not read it, the one thing to carry over is that an interval is a start and an end, like “km 2 to km 6”. Sorting costs a little more than one pass, written O(n log n), and trying every subset of n things costs O(2ⁿ), which doubles with each item you add; Sorting & Searching shows where the first one comes from.

Lesson 68 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The rehearsal room: one room, too many bands
  • Overlap: two bands in the room, and why touching is fine
  • The wrong first idea: first come, first served
  • The second wrong idea: keep the shortest bookings
  • Sort by end: whoever leaves first goes first
  • Why earliest-end is safe: the exchange argument
  • The tell, and the contrast with merging
  • Brute force: try every set of bookings · O(2ⁿ · n)
  • Better: a table of best-so-far · O(n²)
  • Best: sort by end, keep what fits · O(n log n) time, O(n) space
  • The three wrong versions, side by side
  • The by-start version that does work
  • Timings
  • In an interview
  • The interval ladder: easy, medium, hard

Terms it explains: interval, O(n log n), O(2ⁿ), overlap, greedy, exchange argument, Write the pass,, backtracking, brute force, constraint satisfaction, divide & conquer, dynamic programming, Master theorem, pruning, the meta-method.

Lab

Grabbing the biggest coin that fits gives the fewest coins with 1, 5, 10 and 25. Does it with any coins?

Pick 1, 3, 4 under The coins, set The amount to make to 6, and watch Coins greedy wasted.

Open The greedy bet

Review DSA