Task Scheduler with Parallel Workers
Heaps, Sorting & SearchAuto-graded · 19 test casesLast updated
Simulate dispatching sequential tasks to parallel workers and determine when all work completes.
You are managing a task scheduler that assigns work to a pool of identical workers. You receive a list of integers tasks where tasks[i] represents how long the i-th task takes to complete, and an integer workers indicating how many parallel workers are available.
All workers are ready at time 0 and are numbered from 0 to workers - 1.
Tasks must be assigned in the exact order they appear:
- Task
0is assigned first, then task1, and so on. - Each task goes to whichever worker finishes its current work earliest.
- When multiple workers finish at the same time, assign the task to the worker with the smallest index.
- A worker executes its assigned task without interruption for the full duration.
Return the total time from 0 until every task has been completed.
Constraints
0 <= len(tasks) <= 10^51 <= workers <= 10^50 <= tasks[i] <= 10^9- The result may exceed 32-bit integer limits
Examples
Example 1
Input: schedule_tasks([3, 2, 4, 1, 5], 2)
Output: 9
Example 2
Input: schedule_tasks([10, 20, 15], 5)
Output: 20
Example 3
Input: schedule_tasks([], 4)
Output: 0
Hints
Hint 1
Tracking which worker becomes available earliest is key—what data structure excels at repeated minimum extraction?
Hint 2
Python tuples compare element-by-element, so a heap of (time, worker_id) pairs naturally implements both the earliest-time rule and the tie-breaking rule.
Hint 3
The last task assigned is not necessarily the last to finish—track the maximum completion time across all assignments.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires simulating a well-defined scheduling policy rather than optimizing an allocation. Tasks must be dispatched in strict order, and each goes to the worker that becomes free soonest, with ties broken by worker index.
The core challenge is efficiently identifying which worker becomes available first at each step. A min-heap is ideal for this repeated minimum-extraction pattern:
- Represent each worker as a tuple
(available_time, worker_id) - Python compares tuples lexicographically, so the heap naturally orders by available time first, then by worker ID for tie-breaking
- For each task: pop the minimum (earliest available worker), compute its new completion time, update the running maximum, and push it back
An important optimization: only min(workers, len(tasks)) workers can ever receive tasks. If there are more workers than tasks, the excess workers remain idle throughout. Starting with workers 0 through min(workers, n) - 1 captures all workers that will actually be used.
The answer is the maximum completion time across all tasks, not simply the completion time of the last-assigned task. A short task assigned late might finish before a long task assigned early.
Correctness
The heap maintains each worker's next available time. Popping the minimum tuple gives us the worker with the earliest availability and, among ties, the lowest index—exactly matching the specified dispatch rule.
Each task starts as soon as its assigned worker is free (since all tasks are queued at time zero), so the completion time is available_time + task_duration.
Tracking the maximum completion time across all assignments ensures we capture when the entire workload finishes, regardless of assignment order.
Complexity
Time: O(m + n log m) where m = min(workers, n) and n = len(tasks)
- Building the initial heap takes
O(m) - Each of
ntasks performs one heap pop and one heap push, eachO(log m)
Space: O(m) for the heap
Python handles arbitrarily large integers, so results near 10^5 × 10^9 = 10^14 work correctly. Languages with fixed-width integers require 64-bit types.
import heapq
from typing import List
def schedule_tasks(tasks: List[int], workers: int) -> int:
if not tasks:
return 0
num_workers = min(workers, len(tasks))
worker_heap = [(0, i) for i in range(num_workers)]
heapq.heapify(worker_heap)
max_finish = 0
for task_duration in tasks:
available_time, worker_id = heapq.heappop(worker_heap)
completion_time = available_time + task_duration
max_finish = max(max_finish, completion_time)
heapq.heappush(worker_heap, (completion_time, worker_id))
return max_finish