KatInterview

Minimize Maximum Job Lateness with Deadlines

Greedy & IntervalsAuto-graded · 17 test cases
MediumAuto-gradedGreedy & Intervalsgreedyintervalssorting

Last 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 durations is empty, return 0.

Constraints

  • 0 ≤ n ≤ 2000 (where n = len(durations) = len(deadlines))
  • 1 ≤ durations[i] ≤ 1000
  • 1 ≤ 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.