851. Assign Jobs Cheaply

A repair shop has n technicians and n jobs, both numbered from 0. The matrix cost says that technician i charges cost[i][j] to handle job j. Every technician must take exactly one job and every job must be taken by exactly one technician.

Return the smallest possible total charge over all such one-to-one assignments. Costs can be zero, and the matrix is not necessarily symmetric. When n = 1 the answer is simply cost[0][0].

Example 1

Input:
cost = [[7,3,9],[4,8,2],[6,5,1]]
Output:
8
Explanation:

Giving worker 0 job 1, worker 1 job 0 and worker 2 job 2 costs 3+4+1 = 8, and no other pairing is cheaper.

Example 2

Input:
cost = [[5,5],[5,5]]
Output:
10
Explanation:

Every pairing costs 10 because all prices are equal.

Constraints

  • 1 ≤ n ≤ 15, where cost is an n x n matrix
  • 0 ≤ cost[i][j] ≤ 106

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(2^n * n)
Space
O(2^n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…