Minimize Maximum Job Lateness with Deadlines
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests the classic exchange-argument greedy insight: scheduling jobs by earliest deadline minimizes the maximum lateness over all jobs.
Minimize Maximum Job Lateness
You are managing a single machine that processes jobs one at a time. There are n jobs to schedule. Each job i is described by:
duration[i]— the number of time units the job takes to run.deadline[i]— the time by which the job should finish.
You may run the jobs in any order, back-to-back with no gaps (the machine runs continuously starting at time 0). If a job finishes at time f and its deadline is d, its lateness is max(0, f - d) (it is never negative; early or on-time jobs have lateness 0).
Your goal is to choose a job ordering that minimizes the maximum lateness across all jobs, and return that minimum possible maximum lateness.
Details
- All jobs must be scheduled (no job may be skipped).
- Jobs are non-preemptive: once started, a job runs to completion before the next one begins.
- The machine starts at time
0. - If
durationsis empty, return0.
Constraints
0 ≤ n ≤ 2000(wheren = len(durations) = len(deadlines))1 ≤ durations[i] ≤ 10001 ≤ deadlines[i] ≤ 10000- Both lists have the same length.
Examples
Example 1
Input: min_max_lateness([3, 2, 1], [6, 4, 3])
Output: 0
Example 2
Input: min_max_lateness([4, 1, 2], [7, 3, 5])
Output: 0
Example 3
Input: min_max_lateness([], [])
Output: 0
Hints
Hint 1
Try every possible ordering of two adjacent jobs and ask: which job should go first to minimize the worst lateness between the two?
Hint 2
If you swap job A (currently first) with job B (currently second), the only lateness that changes is A's — it gets later — and B's — it gets earlier. Work out when swapping is beneficial.
Hint 3
The exchange argument shows that if A's deadline exceeds B's deadline, swapping always helps or ties. Therefore, sorting all jobs by earliest deadline first is globally optimal.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach: Earliest Deadline First (Exchange Argument)
Intuition
Consider any two adjacent jobs A (scheduled first) and B (scheduled second), and suppose the machine finishes everything before them at time t.
-
Current order (A then B):
- A finishes at
t + d_A, lateness =max(0, t + d_A − D_A) - B finishes at
t + d_A + d_B, lateness =max(0, t + d_A + d_B − D_B)
- A finishes at
-
Swapped order (B then A):
- B finishes at
t + d_B, lateness =max(0, t + d_B − D_B) - A finishes at
t + d_A + d_B, lateness =max(0, t + d_A + d_B − D_A)
- B finishes at
In the current order, A's lateness is ≤ what it becomes in the swap (A gets more time), but B's lateness is ≥ what it becomes in the swap (B gets less time). The only case where swapping could reduce the maximum is when D_A > D_B — then putting B (smaller deadline) first can only help.
More precisely: if D_A > D_B, swapping never increases the maximum lateness and can decrease it. Therefore, any ordering that is not sorted by non-decreasing deadline can be improved (or kept equal) by fixing an adjacent inversion. By the exchange argument, Earliest Deadline First (EDF) is optimal.
Algorithm
- Pair each job's
(deadline, duration)and sort bydeadlineascending. - Simulate the schedule: track
current_time, advance by each job's duration, computemax(0, current_time − deadline), and track the running maximum. - Return the maximum lateness.
Correctness
The exchange argument proves no other ordering yields a strictly smaller maximum lateness. EDF is a well-known optimal solution to this problem; the proof above formalizes why.
Complexity
- Time: O(n log n) for sorting; O(n) for the simulation scan. Overall O(n log n).
- Space: O(n) for the sorted list of pairs.
from typing import List
def min_max_lateness(durations: List[int], deadlines: List[int]) -> int:
if not durations:
return 0
# Exchange-argument greedy: sort by earliest deadline first.
jobs = sorted(zip(deadlines, durations)) # sort by deadline ascending
current_time = 0
max_lateness = 0
for deadline, duration in jobs:
current_time += duration
lateness = max(0, current_time - deadline)
max_lateness = max(max_lateness, lateness)
return max_lateness