Minimum Penalty Task Schedule with Deadlines
Greedy & IntervalsAuto-graded · 17 test casesLast 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) <= 20001 <= processing_time <= 1000for each task1 <= deadline <= 10^6for 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.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach: Exchange-Argument Greedy (Earliest Deadline First)
Intuition
We want to order all tasks to minimize the maximum lateness. The key insight comes from the exchange argument:
Suppose the current schedule has two adjacent tasks A (scheduled first) and B (scheduled second) where deadline(A) > deadline(B) — an "inversion." Let t be the time at which A starts.
- Before swap: finish(A) = t + p_A, finish(B) = t + p_A + p_B
lateness pair = max(0, t+p_A − d_A), max(0, t+p_A+p_B − d_B) - After swap (B first): finish(B) = t + p_B, finish(A) = t + p_A + p_B
lateness pair = max(0, t+p_B − d_B), max(0, t+p_A+p_B − d_A)
Notice that task A's finish time is identical after the swap (t + p_A + p_B). So A's lateness cannot increase. For B, its finish time decreases from (t+p_A+p_B) to (t+p_B), so B's lateness cannot increase either. Therefore, the maximum lateness of the pair never increases when we fix the inversion.
Since any schedule with an inversion can be improved (or kept equal) by removing that inversion, the optimal schedule has no inversions — i.e., tasks sorted by deadline in non-decreasing order. This is the classic Earliest Deadline First (EDF) result for minimizing maximum lateness.
Algorithm
- Sort
tasksbydeadlineascending. - Simulate the schedule: maintain a running clock starting at 0.
- For each task, advance the clock by its processing time, then compute
lateness = max(0, clock − deadline). - Return the maximum lateness seen.
Correctness
By the exchange argument, any ordering that differs from deadline-sorted order contains an adjacent inversion that can be fixed without worsening the objective. Applying this repeatedly (bubble-sort argument) shows the deadline-sorted order is globally optimal.
Complexity
- Time: O(n log n) for sorting, O(n) for the simulation → O(n log n) overall.
- Space: O(n) for the sorted copy (or O(1) extra if sorted in-place).
Reference Solution
from typing import List
def min_max_lateness(tasks: List[List[int]]) -> int:
# Exchange-argument greedy: sort by deadline ascending.
# Proof sketch: for any adjacent pair (A before B) where deadline(A) > deadline(B),
# swapping them cannot increase the maximum lateness of the pair, so the optimal
# schedule has no such "inversion" — i.e., it is sorted by deadline.
sorted_tasks = sorted(tasks, key=lambda t: t[1])
current_time = 0
max_lateness = 0
for processing_time, deadline in sorted_tasks:
current_time += processing_time
lateness = max(0, current_time - deadline)
if lateness > max_lateness:
max_lateness = lateness
return max_lateness