← Lessons

quiz vs the machine

Gold1300

Algorithms

Interval Scheduling Maximization

Pick the most non overlapping jobs by always taking the earliest finish.

4 min read · core · beat Gold to climb

Fitting the most jobs

Suppose many jobs each occupy a time interval and you control one machine that runs one job at a time. Interval scheduling maximization asks for the largest set of jobs you can run without any two overlapping. A greedy rule solves it cleanly.

The earliest finish rule

The winning strategy is surprisingly simple:

  • Sort the jobs by their finishing time, earliest first.
  • Walk the sorted list and take a job whenever it starts after the last taken job ends.
  • Skip any job that would overlap a job already chosen.

Choosing the job that frees the machine soonest leaves the most room for the jobs that follow, which is the intuition behind why this works.

Why greedy is safe here

Sorting by start time or by shortest duration can fail, but earliest finish never does. The proof rests on an exchange argument: any optimal schedule can be transformed step by step into the greedy one without losing any jobs, because swapping in the earliest finishing choice never blocks a later selection. After sorting, a single pass produces an optimal answer.

Key idea

Sorting by earliest finishing time and greedily taking compatible jobs yields the maximum set of non overlapping intervals.

Check yourself

Answer to earn rating on the learn ladder.

1. Which sorting key makes the greedy choice optimal?

2. What kind of proof justifies the greedy rule?

3. Why might sorting by shortest duration fail?