Maximize Tasks Completed Before Their Deadlines
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests whether candidates can apply a greedy scheduling-with-deadlines strategy to select and order tasks that maximize the count of on-time completions.
Task Deadline Scheduler
You are managing a single worker who processes tasks one at a time, starting at time 0. Each task has a duration (how many time units it takes) and a deadline (the task must finish by this time, inclusive).
Given a list of tasks, your goal is to choose a subset of tasks and schedule them in some order so that every chosen task finishes on or before its deadline, maximizing the number of tasks completed.
Return the maximum number of tasks the worker can complete on time.
The worker works continuously with no idle time between scheduled tasks (i.e., the next task starts immediately when the previous one ends).
Formal definition
You are given a list tasks where tasks[i] = [duration_i, deadline_i].
- Find the largest subset
Sof tasks such that there exists an ordering ofSwhere every task inSfinishes on time. - Return
|S|.
How to determine the optimal subset
Use the classic Earliest Deadline First (EDF) greedy principle:
- It is always optimal to schedule chosen tasks in non-decreasing order of deadline.
- To maximise the count of tasks completed, iterate over tasks sorted by deadline and greedily include a task if adding it (at the end of the current schedule) still meets its deadline. If including it violates its deadline, skip it — never swap it for an already-accepted task.
This greedy rule (sort by deadline, include if feasible) is provably optimal for maximising the count of on-time tasks when all tasks have equal priority.
Constraints
0 ≤ len(tasks) ≤ 20001 ≤ duration_i ≤ 10001 ≤ deadline_i ≤ 10000- Tasks with equal deadlines may appear; process them in the order they appear in the sorted list (stable sort by deadline, then by duration ascending as a tie-break — this does not affect correctness but ensures a unique greedy trace).
- An empty task list returns
0.
Examples
Example 1
Input: max_tasks_on_time([[3, 5], [1, 3], [2, 4]])
Output: 2
Example 2
Input: max_tasks_on_time([[5, 4], [2, 6], [1, 2], [3, 9]])
Output: 3
Example 3
Input: max_tasks_on_time([])
Output: 0
Hints
Hint 1
Sort tasks by deadline first — any valid schedule for a chosen subset can be rearranged into EDF order without causing any previously feasible task to miss its deadline (exchange argument).
Hint 2
Keep a running total of time elapsed; when you consider the next task in deadline order, check whether current_time + duration ≤ deadline before including it.
Hint 3
You do not need to swap or replace already-accepted tasks — simply skipping infeasible tasks while iterating in deadline order is sufficient to maximise the count.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight — Exchange Argument
For any subset of tasks that can all be completed on time, there exists a valid schedule that processes them in non-decreasing deadline order (Earliest Deadline First). Proof sketch: if task A with a later deadline is scheduled before task B with an earlier deadline, swapping them cannot cause A to miss its deadline (it finishes no later than before), and can only help B meet its earlier deadline.
This means we never need to consider non-EDF orderings when checking feasibility.
Greedy Algorithm
- Sort all tasks by deadline ascending (tie-break by duration ascending for a deterministic trace).
- Maintain
current_time = 0andcount = 0. - For each task
(duration, deadline)in sorted order:- If
current_time + duration ≤ deadline, include it: adddurationtocurrent_time, incrementcount. - Otherwise, skip it.
- If
- Return
count.
Why skipping (not swapping) is optimal for count
We want to maximise the number of tasks, not total value. A swap (replacing an already-accepted longer task with a shorter skipped task) could increase feasibility of later tasks, but this is handled by the well-known Moore–Hodgson algorithm for minimising lateness. For the simpler problem of maximising count with unit weights, the pure greedy (sort by deadline, include if feasible, skip otherwise) is optimal because:
- Every included task finishes as early as possible given the chosen prefix.
- Skipping a task that would overrun its deadline never prevents us from including a strictly greater number of subsequent tasks.
Complexity
- Time: O(n log n) for the sort, O(n) for the scan — overall O(n log n).
- Space: O(n) for the sorted copy.
def max_tasks_on_time(tasks: list) -> int:
# Sort by deadline ascending; break ties by duration ascending (stable, unique trace)
sorted_tasks = sorted(tasks, key=lambda t: (t[1], t[0]))
current_time = 0
count = 0
for duration, deadline in sorted_tasks:
if current_time + duration <= deadline:
current_time += duration
count += 1
# else: skip this task — cannot finish it on time
return count
Worked example
Tasks [[3,5],[1,3],[2,4]] sorted by deadline → [[1,3],[2,4],[3,5]].
| Step | Task | current_time before | Finish | Deadline | Include? | current_time after |
|---|---|---|---|---|---|---|
| 1 | dur=1,dl=3 | 0 | 1 | 3 | Yes | 1 |
| 2 | dur=2,dl=4 | 1 | 3 | 4 | Yes | 3 |
| 3 | dur=3,dl=5 | 3 | 6 | 5 | No | 3 |
Result: 2.