Maximize Profit from Weighted Deadline-Constrained Tasks
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests the classic weighted job scheduling with deadlines: greedily select a maximum-profit feasible subset of unit-length tasks, each with a deadline and profit, using a disjoint-set (union-find) structure for efficiency.
Task Scheduler with Deadlines
You are managing a single-machine task queue. You are given n tasks, where each task has:
- a profit (positive integer) earned if the task is completed, and
- a deadline (positive integer): the task must be started and finished within time slots 1 through deadline, inclusive.
Each task takes exactly one time slot to complete. The machine can run at most one task per time slot. You may choose any subset of tasks and assign each chosen task to a distinct time slot that does not exceed its deadline.
Your goal is to select a subset of tasks and assign them to time slots so that the total profit is maximized.
Return the maximum total profit achievable.
Details
- Time slots are numbered
1, 2, 3, …. - A task with deadline
dcan be placed in any of the slots1, 2, …, d. - Each slot can hold at most one task.
- You do not have to schedule all tasks; unscheduled tasks contribute 0 profit.
- If no tasks can be scheduled (e.g., the list is empty), return
0.
Constraints
0 ≤ n ≤ 2000(number of tasks)1 ≤ profit ≤ 10^6for each task1 ≤ deadline ≤ nfor each task (deadlines are capped atnsince scheduling beyondnslots is never necessary)tasksis a list of[profit, deadline]pairs (each a two-element list of ints)
Examples
Example 1
Input: max_profit_scheduling([[100, 2], [19, 1], [27, 2], [25, 1], [15, 3]])
Output: 142
Example 2
Input: max_profit_scheduling([[50, 1], [10, 1], [20, 1]])
Output: 50
Example 3
Input: max_profit_scheduling([])
Output: 0
Hints
Hint 1
Sort tasks in decreasing order of profit; greedily try to schedule each task in the latest available slot at or before its deadline.
Hint 2
To efficiently find the latest free slot ≤ d, use a union-find (disjoint set union) structure where each slot points to the latest free slot at or before it.
Hint 3
When a slot s is occupied, union it with slot s−1 so that future queries for slot s immediately redirect to the next free slot below s.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Greedy by Profit + Union-Find for Slot Allocation
This is the classic Job Sequencing with Deadlines problem solved optimally.
Greedy argument (exchange argument):
Sort tasks by profit in descending order. Consider any optimal schedule O. If our greedy solution G differs from O, there is a task t_G in G not in O and a task t_O in O not in G, where profit(t_G) ≥ profit(t_O) (because we process higher-profit tasks first). We can always swap t_O out and put t_G in (or leave the slot empty), never decreasing profit. By repeated exchanges, O can be transformed into G without losing profit, so G is optimal.
Algorithm:
- Sort all tasks by profit descending.
- For each task, attempt to place it in the latest available slot ≤ deadline (to preserve earlier slots for tasks with tighter deadlines — though because we process by profit, slot choice only needs to find any free slot ≤ deadline; choosing the latest one is a clean convention).
- Use a Union-Find (DSU) structure where
find(s)returns the latest free time slot at or befores. Slot0is a sentinel meaning "no slot available." - When slot
sis filled, setparent[s] = s - 1so futurefind(s)calls skip to the next free slot below.
Correctness
- Greedy correctness follows from the exchange argument above.
- Union-Find correctly tracks the latest free slot ≤ any query slot with path compression, so we never double-book a slot.
Complexity
- Time: O(n log n) for sorting + O(n · α(n)) ≈ O(n log n) overall, where α is the inverse Ackermann function from path-compressed union-find.
- Space: O(n) for the parent array.
Edge Cases
- Empty input → return
0. - All tasks share the same deadline → only the top-
deadlineby profit can be scheduled. - All deadlines are
1→ only one task (the highest profit) can be scheduled. - All profits equal → schedule as many tasks as possible (up to the number of distinct slots available).
from typing import List
def max_profit_scheduling(tasks: list) -> int:
if not tasks:
return 0
n = len(tasks)
# Sort tasks by profit descending (greedy: always try highest-profit first)
sorted_tasks = sorted(tasks, key=lambda x: -x[0])
# Union-Find: parent[s] = latest free slot <= s
# Slot 0 is a sentinel meaning "no free slot available"
parent = list(range(n + 1))
def find(s):
while parent[s] != s:
parent[s] = parent[parent[s]] # path compression
s = parent[s]
return s
total_profit = 0
for profit, deadline in sorted_tasks:
# Find the latest free slot at or before deadline
free_slot = find(deadline)
if free_slot == 0:
# No free slot available at or before this deadline
continue
# Schedule the task in free_slot
total_profit += profit
# Mark free_slot as used: union it with free_slot - 1
parent[free_slot] = free_slot - 1
return total_profit