AlgoFrames

Minimum cost assignment

Standard · Write code · about 18 min

costs[i][j] is the cost of giving task j to worker i. There are as many workers as tasks, and every worker gets exactly one task, with no task used twice.

Return the smallest possible total cost.

Example 1

Input:

[[[9,2,7],[6,4,3],[5,8,1]]]

Output:

9

Worker 0 takes task 1 (2), worker 1 task 2 (3) and worker 2 task 0 (5): total 9.

Example 2

Input:

[[[3,1],[2,5]]]

Output:

3

Worker 0 takes task 1 (1), worker 1 takes task 0 (2).

Constraints

  • 1 ≤ costs.length ≤ 12
  • costs is square: costs[i].length = costs.length
  • 0 ≤ costs[i][j] ≤ 100

Learn the idea first: Bitmask DP: TSP and assignments, a visual lesson.

More Bitmask DP: TSP and assignments problems