The Find the Minimum Cost Array Permutation interview question asks you to find a permutation of the integers such that a specific cost function is minimized. The cost is usually defined as the sum of for all , wrapping around the array. You also need to return the lexicographically smallest permutation that achieves this minimum cost. Note that can always be fixed to 0 to simplify the search.
Google uses this "Hard" problem to test mastery of Dynamic Programming with Bitmasks and the Traveling Salesperson Problem (TSP) logic. The permutation is essentially a path through all numbers, and the cost depends on the transition between adjacent elements. It evaluations whether you can optimize a factorial search into an exponential one.
This is a Bitmask Dynamic Programming problem.
dp[mask][last_val] is the minimum cost to have visited the set of numbers in mask, with last_val being the most recent addition.next_val not in the mask:
dp[mask | (1 << next_val)][next_val] = min(..., dp[mask][last_val] + cost(last_val, next_val)).next_val.parent or choice table to reconstruct the path from the DP table.fixed. Possible permutations: [0, 1, 2] and [0, 2, 1].
"Permutation of items" + "Cost depends on adjacent pairs" + "" is the classic signature of Bitmask DP. Practice the TSP (Traveling Salesperson) problem as this is a direct variation of it.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum AND Sum of Array | Hard | Solve | |
| Smallest Sufficient Team | Hard | Solve | |
| Concatenated Divisibility | Hard | Solve | |
| Minimum Time to Kill All Monsters | Hard | Solve | |
| Minimum XOR Sum of Two Arrays | Hard | Solve |