A worked example: the most pay from jobs that can't overlap
Maximum Profit in Job Scheduling is on Airbnb's LeetCode tag lists: the all-time, six-month and three-month ones (3 sources). In our setting, one GPU runs render jobs, one at a time. Each job has a start hour, an end hour and what it pays. A job can start at the hour another ends. Pick the jobs that pay the most in total.
Jobs (start, end, pay): (0, 4, 60), (1, 5, 70), (4, 6, 40), (7, 10, 30), (6, 11, 20), (9, 14, 100), no two at once. What's the most pay?
Here they are on a line of hours, sorted by end time:
The jobs, sorted by end time
(0, 4) pays 60
(1, 5) pays 70
(4, 6) pays 40
(7, 10) pays 30
(6, 11) pays 20
(9, 14) pays 100
046914
hours
Green is the best set: 60 + 40 + 100 = 200.
Clarify first. Say it out loud: a job that ends at 4 and one that starts at 4 can both run. Every job ends after it starts. Pay is positive. The jobs come in any order. Return the total, not the list, unless they ask.
Give the obvious answers. Try every subset: 2n of them. That took 3 ms for 12 jobs, 13 ms for 14 and 61 ms for 16, four times as long for every two more jobs.
Then the greedy ideas, which are worth naming because they fail:
Highest pay first takes 100, then 70, and nothing else fits: 170.
Earliest end first, the rule that fits the most jobs, takes 60, 40 and 30: 130.