Lv.3bitmaskdp
Minimum-Cost Task Assignment (Small)
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
There are N employees and N tasks; cost[i][j] is the cost of employee i doing task j. Assign each employee
exactly one task, all tasks different, and return the minimum possible total cost.
Examples
| cost | result |
|---|---|
[[9, 2, 7], [6, 4, 3], [5, 8, 1]] | 9 |
[[4]] | 4 |
[[1, 2], [1, 100]] | 3 |
Example 1 — Employee 0 → task 1 (2), employee 1 → task 0 (6), employee 2 → task 2 (1).
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(cost: int[][]) → int
Constraints
-
cost.length,cost[i].length -
cost[i][j]
Expected complexity
- Time
- O(N * 2^N)
- Space
- O(2^N)
Examples
| cost | Expected |
|---|---|
[[9,2,7],[6,4,3],[5,8,1]] | 9 |
[[4]] | 4 |
[[1,2],[1,100]] | 3 |
Hint
Hint
Assign employees in order 0, 1, …; only the set of used tasks matters. With that set as a bitmask mask, the next
employee is popcount(mask), and dp[mask | 1<<j] = min(dp[mask] + cost[popcount(mask)][j]) over unused j. Trying all
permutations exceeds the time limit; is enough.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 2548 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 11 | verdict only |
| performance | 60 | 5 | verdict only |