7 routes42 sections324 units

Unit 4.4 · about 4 h

116 / 324

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.

  • derive
  • implement

You can now

  • 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 \(O(n \log n)\) time and \(O(1)\) 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 rules
  • Interval, scheduling and covering problems

Study, in this order

3 steps

  1. Interval scheduling: the comparator is the algorithm
    DSA 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 half
    DSA Handbook - recursion, backtracking and greedy

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

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

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

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

Focus on

  • The comparator is the algorithm: sort by end time, keep an interval exactly when its \(\text{start}\) is at least the \(\text{current end}\). 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 \(\text{start} < \text{end}\)), while LC 56 merges them (\(\text{start} \le \text{end}\), then extend with max). Pasting one boundary into the other is the standard bug.

Reproduce closed-book

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.

Traps

  • 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.

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.

Self-test

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

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

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

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

Completion criterion

Stop when

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

Mark passed, then open 4.5 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

Also in this section

  • AlgoMaster DSA - decomposition and greedy slice
    AlgoMasterrequiredpractice
    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 algorithms
    MIT OpenCourseWaresupporttheory
    Use the probability, expectation and concentration readings to derive sampling probabilities and distinguish expected from high-probability guarantees.
  • Algorithms lecture notes - randomized algorithms
    Jeff Ericksonsupporttheory
    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.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels