Assign Tasks to Time Slots with Minimum Idle Penalty
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests the exchange-argument greedy technique by scheduling deadline-bearing tasks into time slots to minimize total weighted idle (missed-deadline) penalty.
Assign Tasks to Time Slots with Minimum Idle Penalty
You are managing a single-worker pipeline. The worker processes one task per time slot, using slots numbered 1, 2, 3, ….
You are given n tasks. Each task i is described by two values:
deadlines[i]— the latest slot by which taskimust finish (1-indexed, inclusive). If the task is not scheduled in any slot≤ deadlines[i], it incurs a penalty.penalties[i]— the non-negative integer penalty incurred if taskimisses its deadline.
You may schedule at most one task per slot. You do not have to schedule every task; any task left unscheduled also incurs its full penalty.
Your goal is to select a subset of tasks to schedule (one per slot, each in a slot ≤ its deadline) so that the total penalty of all unscheduled/missed tasks is minimized.
Return the minimum total penalty.
How the greedy works (hint-free version)
Think carefully about which tasks are worth keeping "on time" and which you are willing to sacrifice — and in what order you should make that decision.
Constraints
1 ≤ n ≤ 2000wheren = len(deadlines) = len(penalties)1 ≤ deadlines[i] ≤ n0 ≤ penalties[i] ≤ 10^6- Both lists have the same length.
- If
deadlinesandpenaltiesare empty, return0.
Examples
Example 1
Input: min_idle_penalty([2, 1, 2, 1, 3], [20, 15, 10, 5, 1])
Output: 15
Example 2
Input: min_idle_penalty([1, 1, 1], [10, 30, 20])
Output: 30
Example 3
Input: min_idle_penalty([], [])
Output: 0
Hints
Hint 1
Sort tasks by deadline first. Among tasks with the same deadline, think about which one you'd rather drop if you can only keep a limited number.
Hint 2
Use an exchange argument: suppose you've decided on a set of on-time tasks. If a slot is over-committed (more tasks than slots ≤ their deadline), dropping the task with the smallest penalty in that group costs the least.
Hint 3
Maintain a min-heap of penalties for tasks you've tentatively scheduled. When adding a new task would exceed the available slots up to its deadline, evict the task with the smallest penalty from the heap (it joins the 'missed' pile).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight (Exchange Argument)
We want to choose a maximum-weight subset of tasks that can be feasibly scheduled — one per slot — so the total penalty of the unchosen tasks is minimised (equivalently, the total penalty of the chosen tasks is maximised).
Exchange argument: Suppose we have any feasible schedule S. If we can swap out a lower-penalty task for a higher-penalty task without violating feasibility, we strictly improve. Repeating this argument shows the optimum always keeps the highest-penalty feasible subset.
Algorithm
- Sort tasks by deadline ascending (ties broken arbitrarily — the heap handles correctness).
- Sweep through tasks in this order, maintaining a min-heap of penalties of tentatively scheduled tasks and a counter
scheduledof how many tasks are in the heap. - For each task
(d, p):- If
scheduled < d: there is still a free slot ≤d, so pushponto the heap and incrementscheduled. - Otherwise all
dslots are taken. Comparepwith the heap minimum:- If
heap[0] < p: the cheapest already-scheduled task is less valuable; evict it (add its penalty tototal_penalty) and replace it with this task. - Else: drop this task and add
ptototal_penalty.
- If
- If
- Return
total_penalty.
Why It Is Correct
Processing tasks in deadline order ensures that when we consider task (d, p), all previously considered tasks have deadline ≤ d. The heap always contains exactly scheduled tasks that can be placed in slots 1…(max_deadline_seen). When a slot conflict arises, the exchange argument guarantees that evicting the minimum-penalty task is safe: any valid schedule that kept that task instead of the current one can be converted to one that keeps the current task instead, with equal or less total penalty.
Complexity
- Time: O(n log n) — one sort plus n heap operations each O(log n).
- Space: O(n) for the heap.
import heapq
from typing import List
def min_idle_penalty(deadlines: List[int], penalties: List[int]) -> int:
n = len(deadlines)
if n == 0:
return 0
tasks = sorted(zip(deadlines, penalties), key=lambda x: (x[0], -x[1]))
heap = []
scheduled = 0
total_penalty = 0
for deadline, penalty in tasks:
if scheduled < deadline:
heapq.heappush(heap, penalty)
scheduled += 1
else:
if heap and heap[0] < penalty:
total_penalty += heapq.heapreplace(heap, penalty)
else:
total_penalty += penalty
return total_penalty