KatInterview

Minimum Maximum Workload: Assign Tasks to Workers

Heaps, Sorting & SearchAuto-graded · 17 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary searchgreedysorting

Last 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 ≤ 1000
  • 1 ≤ len(durations) ≤ 2000
  • 1 ≤ durations[i] ≤ 10^6
  • k may be greater than or equal to len(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.