cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 140 of 218
Ideas this builds on
Greedy choices140 / 218

Why earliest finish earns a commitment

Picture

Choose the largest number of non-overlapping appointments on one calendar. Every appointment counts equally. Taking the compatible appointment that finishes earliest leaves the remaining calendar as open as possible. To trust that sentence, show how an optimal schedule can be changed to start with your choice.

Predict

Suppose a best possible schedule starts with appointment O, while greedy chooses G, which finishes no later than O. What happens to the later appointments if you replace O with G?

Reveal the reasoning

They still fit: every later appointment starts at or after O finishes, and G finishes no later. The replacement preserves the appointment count. Thus at least one optimal schedule starts with the greedy choice.

Go deeper: exchange, then repeat on the remainder

After making the replacement, solve the remaining appointments beginning at or after G ends. Repeating the same argument justifies every commitment. The proof does not claim all optimal schedules are identical.

Intervals are half-open [start,end), with start < end. Therefore one interval ending at time 2 is compatible with another starting at 2.

def schedule(intervals):
    chosen = []
    finish = float("-inf")
    for start, end in sorted(intervals, key=lambda interval: interval[1]):
        if start >= finish:
            chosen.append((start, end))
            finish = end
    return chosen

assert schedule([(0, 10), (1, 2), (2, 3)]) == [(1, 2), (2, 3)]
assert schedule([(1, 2), (2, 3)]) == [(1, 2), (2, 3)]
assert schedule([]) == []

Sorting costs O(n log n) and the scan O(n). This Python implementation makes a sorted copy and returns up to n selected intervals, so total additional storage is O(n).

Why not earliest start? One early long interval can block several short ones. Why not shortest duration? (0,3), (3,6), (2,4) makes the shortest interval block two compatible longer ones. Finishing early is the specific property the exchange proof uses.

Recognition signal: can you transform an arbitrary optimum to contain your first choice without reducing its objective or breaking feasibility? An explicit transformation is stronger than “it leaves room.”

Analogy limit: leaving more time is sufficient here because all appointments have equal value and there is one resource. Other objectives and constraints can invalidate the exchange.

Change one rule

Appointments pay different amounts and you want maximum income. Does replacing the first appointment preserve the objective?

Check the variation

Not necessarily. The earlier-finishing appointment may pay less. The proof preserved count, not income, so it no longer establishes optimality.

Connect

Weighted interval DP solves the changed objective. Dijkstra has a different safe-commitment argument. Recognition asks you to name the assumption a proof consumes.