KatInterview

Minimum Penalty Task Schedule with Deadlines

Greedy & IntervalsAuto-graded · 17 test cases
HardAuto-gradedGreedy & Intervalsgreedyintervalsscheduling

Last updated

Tests the classic exchange-argument greedy insight that scheduling jobs by deadline minimizes total weighted lateness on a single machine.

Minimum Penalty Task Schedule with Deadlines

You are managing a single worker who processes tasks one at a time with no gaps or preemption. Each task has a processing time and a deadline. The worker starts at time 0 and must complete all tasks.

For each task, define its lateness as:

lateness(i) = max(0, finish_time(i) - deadline(i))

That is, how many time units past its deadline the task finishes (0 if it finishes on or before its deadline).

Your goal is to choose an ordering of the tasks that minimizes the maximum lateness across all tasks.

Given a list of tasks where tasks[i] = [processing_time, deadline], return the minimum possible maximum lateness.

How to break ties

When multiple orderings achieve the same minimum maximum lateness, the answer (the lateness value itself) is unique — you only need to return the single integer minimum-maximum-lateness value, so no tie-breaking in the output is needed.

Constraints

  • 1 <= len(tasks) <= 2000
  • 1 <= processing_time <= 1000 for each task
  • 1 <= deadline <= 10^6 for each task
  • All values are positive integers.
  • The list may contain tasks with identical deadlines or identical processing times.
  • The answer fits in a 32-bit signed integer.

Examples

Example 1

Input: min_max_lateness([[3, 6], [2, 9], [1, 8], [4, 4], [3, 12]])

Output: 1

Example 2

Input: min_max_lateness([[5, 5]])

Output: 0

Example 3

Input: min_max_lateness([[2, 10], [2, 10], [2, 10]])

Output: 0

Hints

Hint 1

Think about what happens when you swap two adjacent tasks in the schedule — can you characterize when a swap improves (or cannot worsen) the maximum lateness?

Hint 2

If task A is scheduled before task B, and deadline(A) > deadline(B), swapping them never increases the maximum lateness. This is the exchange argument: any schedule with an inversion can be improved or kept the same by fixing that inversion.

Hint 3

Sort all tasks by deadline in non-decreasing order, then simulate the schedule by accumulating finish times and computing each task's lateness; the answer is the maximum of those lateness values.