Minimum Penalty Class Schedule with Fixed Breaks
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests whether a candidate can apply an exchange-argument greedy to order tasks with deadlines and penalties to minimize total weighted lateness, with fixed unavailable time windows.
Minimum Penalty Class Schedule with Fixed Breaks
You are scheduling a sequence of study sessions for a student. The day is divided into unit time slots numbered 1, 2, 3, …. The student must complete n study tasks. Each task takes exactly 1 time slot to complete.
Some time slots are blocked (the student has fixed commitments — meals, exercise, etc.) and cannot be used for any task. All tasks must be scheduled in unblocked slots, one task per slot.
Each task i has:
- a deadline
deadlines[i]— the last time slot by which the task should finish. - a penalty
penalties[i]— a positive integer cost incurred per unit of lateness if the task finishes after its deadline.
If task i is assigned to time slot t, its lateness is max(0, t - deadlines[i]), and it contributes penalties[i] * max(0, t - deadlines[i]) to the total cost.
Your goal is to assign each task to a distinct unblocked slot and minimize the total weighted lateness.
Exchange-argument insight: Among all schedules that use the same set of slots, the optimal ordering is determined by a classic greedy rule. Your challenge is to apply that rule correctly even when the available slots are not contiguous.
Inputs
deadlines: list ofnpositive integers,deadlines[i]is the deadline for taski.penalties: list ofnpositive integers,penalties[i]is the per-unit lateness cost for taski.blocked: list of distinct positive integers — the blocked time slots. May be empty.
The scheduler always uses the first n available (unblocked) slots to complete all tasks (there is no benefit to using a later slot when an earlier one is free).
Task
Return the minimum total weighted lateness achievable by ordering the tasks across those first n unblocked slots.
How ties are broken
When two tasks have the same penalty-per-unit, order them by earlier deadline first (lower deadlines[i] first). This ensures a unique optimal solution.
Constraints
1 ≤ n ≤ 2000(number of tasks)1 ≤ deadlines[i] ≤ 10^61 ≤ penalties[i] ≤ 10^40 ≤ len(blocked) ≤ 500- All values in
blockedare distinct positive integers. - The
blockedlist is not necessarily sorted. - Blocked slots may fall within or beyond the range of slots used; only the first
nunblocked slots matter.
Examples
Example 1
Input: min_weighted_lateness([3, 1, 4], [10, 5, 2], [])
Output: 5
Example 2
Input: min_weighted_lateness([2, 2, 2], [3, 3, 3], [1])
Output: 9
Example 3
Input: min_weighted_lateness([5, 5, 5, 5], [4, 4, 4, 4], [2, 4])
Output: 4
Hints
Hint 1
First, compute the actual time slots you will use: iterate from slot 1 upward, skipping blocked slots, and collect the first n available slots.
Hint 2
The classic exchange argument for weighted lateness scheduling says: if you swap two adjacent tasks i and j (with j after i) and the swap lowers cost, you should do it. Working out the algebra shows the optimal order is decreasing penalty, with ties broken by earlier deadline.
Hint 3
Sort tasks by penalty descending (deadline ascending on ties), then assign the highest-penalty task to the earliest available slot, second-highest to the second slot, etc. The available slots are fixed regardless of ordering, so this greedy is straightforward to apply.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight — Exchange Argument
Fix the set of time slots we will use (always the first n unblocked slots). The question is purely: in what order should we assign tasks to those slots?
Consider two tasks i and j assigned to consecutive slots t and t+1. The cost of placing i first and j second is:
p_i * max(0, t - d_i) + p_j * max(0, t+1 - d_j)
versus placing j first:
p_j * max(0, t - d_j) + p_i * max(0, t+1 - d_i)
Ignoring the deadline clamping for the comparison (the exchange argument works in full generality but the direction is the same), the marginal cost difference reduces to: place the higher-penalty task in the earlier slot. If penalties are equal, the deadline tie-break (earlier deadline first) avoids any extra lateness from the earlier slot.
Therefore the globally optimal schedule is:
Sort tasks by penalty descending; break ties by deadline ascending.
Algorithm
-
Collect available slots. Iterate
slot = 1, 2, 3, …, skipping any inblocked_set, untilnslots are gathered. O(n + max_blocked). -
Sort tasks. Sort by
(-penalty, deadline). O(n log n). -
Assign and accumulate. The task at rank
kin the sorted order getsavailable_slots[k]. Addpenalty * max(0, slot - deadline)for each. O(n).
Correctness
The exchange argument proves that any departure from this ordering can be locally improved without making anything else worse, so the greedy is globally optimal.
Complexity
- Time: O(n log n + B) where B = len(blocked)
- Space: O(n + B)
Reference Solution
from typing import List
def min_weighted_lateness(deadlines: List[int], penalties: List[int], blocked: List[int]) -> int:
n = len(deadlines)
# Step 1: find the first n unblocked slots
blocked_set = set(blocked)
available_slots = []
slot = 1
while len(available_slots) < n:
if slot not in blocked_set:
available_slots.append(slot)
slot += 1
# Step 2: sort tasks by penalty descending, break ties by deadline ascending
task_indices = sorted(range(n), key=lambda i: (-penalties[i], deadlines[i]))
# Step 3: assign tasks to slots in order and compute cost
total = 0
for rank, task_idx in enumerate(task_indices):
t = available_slots[rank]
lateness = max(0, t - deadlines[task_idx])
total += penalties[task_idx] * lateness
return total