KatInterview

Minimum Penalty Class Schedule with Fixed Breaks

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

Last 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 of n positive integers, deadlines[i] is the deadline for task i.
  • penalties: list of n positive integers, penalties[i] is the per-unit lateness cost for task i.
  • 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^6
  • 1 ≤ penalties[i] ≤ 10^4
  • 0 ≤ len(blocked) ≤ 500
  • All values in blocked are distinct positive integers.
  • The blocked list is not necessarily sorted.
  • Blocked slots may fall within or beyond the range of slots used; only the first n unblocked 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.