Unit 4 of 6 · about 4 hours · derive, implement

Greedy counterexamples and interval scheduling

Interval scheduling is the rare greedy proof you can carry in your head. Counterexamples are the tool that stops the wrong greedy rule before it ships.

You'll be able to

  • Construct a minimal counterexample that separates two plausible greedy rules on the same problem.
  • Prove earliest-finish interval scheduling with the exchange argument and state its time and extra space.
  • Implement earliest-finish from the comparator contract and differentially test it against brute force on small inputs with pytest.

Covers

Minimal counterexamples to plausible greedy rulesInterval, scheduling and covering problems
1

Read

In this order, only the parts listed

0 of 3 done

  1. Interval scheduling: the comparator is the algorithmDSA Handbook - recursion, backtracking and greedy

    Why the obvious approach finds the wrong answer; Why sort-by-end is optimal; Worked example: LC 435 on [[1,2], [2,3], [3,4], [1,3]]; Sort-by-start, fold while overlapping (LC 56)

  2. Greedy thinking: the failure halfDSA Handbook - recursion, backtracking and greedy

    Where greedy fails, with coins [1, 3, 4] and the decision rule

    Read only the failure section and the decision rule. The proof templates were covered in the previous unit.

  3. Scheduling Classes, Section 4.2Algorithms - recursion, backtracking and greedy

    4.2 Scheduling Classes (printed pp. 161-164): the earliest-finish rule and its exchange argument

2

Focus on

  • The comparator is the algorithm: sort by end time, keep an interval exactly when its is at least the . Touching intervals are allowed in LC 435.
  • The exchange step replaces the optimal first interval with the earliest-finishing one: it ends no later, so the rest stays disjoint, and induction finishes the proof.
  • Sort-by-start fails on nesting: [[1, 100], [2, 3], [3, 4]] keeps the long interval and removes 2, while the optimum keeps the two short ones and removes 1.
  • LC 435 and LC 56 differ at the boundary: LC 435 treats touching intervals as disjoint (overlap means ), while LC 56 merges them (, then extend with max). Pasting one boundary into the other is the standard bug.
Common mistakes · 3
  • Sorting by start or by duration. Start order fails on nesting ([[1, 100], [2, 3], [3, 4]]); shortest-first fails when a short interval straddles two disjoint ones ([[1, 5], [4, 7], [6, 10]] keeps 1 against 2). Friendly samples pass both, so the test family needs both shapes.
  • Treating touching intervals as overlapping in LC 435. [[1, 4], [4, 5]] removes nothing under LC 435 but merges under LC 56; the two problems also sort differently, by end and by start.
  • Trusting sample tests for a greedy rule. Shrink each candidate rule against the others until the smallest input where they disagree, and check nested and touching cases first.
3

Try it yourself

Closed-book, before checking anything

From a blank file in Python 3.13.14, implement earliest-finish: [[1, 2], [2, 3], [3, 4], [1, 3]] returns 1 removal. A pytest loop over 500 small instances from random.Random(7) agrees with the brute-force maximum on all 500 cases.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Give the earliest-finish exchange step in one move.Answer

    Let be the earliest-finishing interval and the first interval of an optimal set. ends no earlier than , so replacing with keeps the set disjoint and no smaller; induct on the rest.

  2. 2.Give the minimal counterexample to sort-by-start for maximum non-overlapping intervals.Answer

    [[1, 100], [2, 3], [3, 4]]. Start order keeps [1, 100] and removes 2. The optimum keeps [2, 3] and [3, 4] and removes 1.

  3. 3.What does [[1, 4], [4, 5]] give under LC 435 and under LC 56?Answer

    LC 435 removes 0 because touching is allowed. LC 56 merges to [1, 5]. The overlap tests differ only at equality.

  4. 4.How do you hunt a counterexample to a new greedy rule?Answer

    Shrink to the smallest input where two candidate rules disagree, trying nested, touching, and equal-endpoint cases first. A rule that survives only sorted samples is not proved.

5

Practice

  • LeetCode 435: Non-overlapping Intervals

    Examples: [[1, 2], [2, 3], [3, 4], [1, 3]] returns 1; [[1, 2], [1, 2], [1, 2]] returns 2.

    After submitting, read the DSA Handbook interval-scheduling chapter and the AlgoMaster Non-overlapping Intervals lesson (https://algomaster.io/learn/dsa/non-overlapping-intervals), and confirm your sort key and boundary match theirs.

You're done when

LC 435 is accepted, and your differential test plus a nested-interval case both pass.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 3 more in the section

Also in this section

  • AlgoMaster DSA - decomposition and greedy sliceAlgoMaster · required

    Attempt N-Queens and Gas Station cold, then implement reservoir sampling and test its output distribution over fixed-seed trials. Add another judged problem only for a diagnosed weakness.

  • MIT 6.042J - probability and randomized algorithmsMIT OpenCourseWare · support

    Use the probability, expectation and concentration readings to derive sampling probabilities and distinguish expected from high-probability guarantees.

  • Algorithms lecture notes - randomized algorithmsJeff Erickson · support

    Use note 1 Discrete Probability, note 2 Nuts and Bolts (randomized quicksort, with QuickSelect in its exercises), note 4 Tail Inequalities and note 5 Hashing to derive expected costs, separate expected from high-probability guarantees, and analyze hashing behavior.