Minimum Maximum Workload: Assign Tasks to Workers
Heaps, Sorting & SearchAuto-graded · 17 test casesLast updated
Tests ability to binary search on the answer to find the minimum possible maximum workload when distributing tasks among workers.
Minimum Maximum Workload
You are managing a project with n tasks, each with a known duration (in hours). You must assign every task to exactly one of k workers. Each worker processes their assigned tasks sequentially, so their total workload is the sum of durations of all tasks assigned to them.
Your goal is to minimize the maximum workload across all workers — that is, find an assignment such that the busiest worker is as least busy as possible.
Write a function that returns this minimum possible maximum workload.
Assignment rules:
- Every task must be assigned to exactly one worker.
- A worker may receive zero tasks (their workload is
0). - Tasks are indivisible — you cannot split a single task between workers.
- You may assign tasks in any order to any worker.
Approach hint: Think about what values the answer could take, and whether a given target maximum workload is achievable.
Constraints
1 ≤ k ≤ 10001 ≤ len(durations) ≤ 20001 ≤ durations[i] ≤ 10^6kmay be greater than or equal tolen(durations)(some workers may get no tasks)- All durations are positive integers
Examples
Example 1
Input: min_max_workload([3, 6, 1, 4, 2], 3)
Output: 6
Example 2
Input: min_max_workload([10, 10, 10, 10], 2)
Output: 20
Example 3
Input: min_max_workload([5], 4)
Output: 5
Hints
Hint 1
Binary search on the answer: the minimum possible maximum workload lies between max(durations) (lower bound) and sum(durations) (upper bound).
Hint 2
To check if a given maximum workload `W` is feasible, greedily assign tasks (sorted descending) to workers using a first-fit decreasing approach — count how many workers are needed.
Hint 3
If the number of workers needed is ≤ k, then W is achievable. Find the smallest such W using binary search.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Binary Search on the Answer
The answer (minimum possible maximum workload) is some integer in the range [max(durations), sum(durations)]:
- It cannot be smaller than
max(durations), because some worker must handle the largest task alone (if necessary). - It is at most
sum(durations), because one worker could do everything.
We binary search over this range for the smallest value W such that it is feasible to assign all tasks to at most k workers where no worker exceeds workload W.
Feasibility Check (Greedy)
Given a candidate maximum load W, we check greedily whether k workers suffice:
- Sort tasks in descending order (largest first — this is a standard first-fit decreasing heuristic that minimizes the number of workers needed).
- Assign each task to the current worker if it fits (current load + task ≤ W); otherwise, open a new worker.
- If the total workers needed ≤
k, thenWis feasible.
Why greedy works for the feasibility check: We want to know if any assignment stays within W. The greedy first-fit decreasing approach is optimal for bin counting when all bins have the same capacity W — it never wastes capacity unnecessarily.
Binary Search
lo = max(durations)
hi = sum(durations)
while lo < hi:
mid = (lo + hi) // 2
if feasible(mid): hi = mid
else: lo = mid + 1
return lo
This converges to the smallest feasible W.
Complexity
- Sorting: O(n log n)
- Each feasibility check: O(n) (one pass after sorting)
- Binary search iterations: O(log(sum(durations))) ≤ O(log(n · max_d)) ≤ ~O(log(2 × 10⁹)) ≈ 31
- Total: O(n log n + n · log(n · max_d)) = O(n log(n · max_d))
- Space: O(n) for the sorted list
Reference Solution
from typing import List
def min_max_workload(durations: List[int], k: int) -> int:
def feasible(max_load: int) -> bool:
workers_needed = 1
current = 0
for d in sorted_desc:
if current + d <= max_load:
current += d
else:
workers_needed += 1
current = d
return workers_needed <= k
sorted_desc = sorted(durations, reverse=True)
lo = max(durations)
hi = sum(durations)
while lo < hi:
mid = (lo + hi) // 2
if feasible(mid):
hi = mid
else:
lo = mid + 1
return lo