Find the Minimum Cost Array Permutation - Problem

You're given an array nums which is a permutation of [0, 1, 2, ..., n - 1]. Your task is to find the optimal arrangement of indices that minimizes a special scoring function.

The score of any permutation perm is calculated as a circular sum:

score(perm) = |perm[0] - nums[perm[1]]| + |perm[1] - nums[perm[2]]| + ... + |perm[n-1] - nums[perm[0]]|

In other words, we're comparing each position in the permutation with the value at the next position (wrapping around to the beginning). Your goal is to find the permutation that minimizes this total cost.

If multiple permutations achieve the same minimum score, return the lexicographically smallest one. This is a classic optimization problem that combines graph theory with dynamic programming!

Input & Output

example_1.py โ€” Python
$ Input: [1, 3, 0, 2]
โ€บ Output: [0, 2, 3, 1]
๐Ÿ’ก Note: For nums = [1,3,0,2], the optimal permutation is [0,2,3,1]. The score is |0-nums[2]| + |2-nums[3]| + |3-nums[1]| + |1-nums[0]| = |0-0| + |2-2| + |3-3| + |1-1| = 0. This gives the minimum possible score.
example_2.py โ€” Python
$ Input: [0, 1]
โ€บ Output: [0, 1]
๐Ÿ’ก Note: For nums = [0,1], we try both permutations: [0,1] gives score |0-nums[1]| + |1-nums[0]| = |0-1| + |1-0| = 2. [1,0] gives score |1-nums[0]| + |0-nums[1]| = |1-0| + |0-1| = 2. Both have the same score, so we return the lexicographically smaller [0,1].
example_3.py โ€” Python
$ Input: [0]
โ€บ Output: [0]
๐Ÿ’ก Note: With only one element, there's only one possible permutation [0], and its score is |0-nums[0]| = |0-0| = 0.

Visualization

Tap to expand
Circular Permutation as Graph Optimization0123|0-nums[1]||1-nums[2]||2-nums[3]||3-nums[0]|Key Insight: Complete Graph with Circular Constraintโ€ข Each position is a vertex, costs are edge weightsโ€ข Must visit all vertices exactly once (Hamiltonian cycle)โ€ข DP with bitmask avoids O(n!) brute force explosion
Understanding the Visualization
1
Model as Graph Problem
Create a complete graph where vertices are positions 0,1,...,n-1 and edge weights are |i - nums[j]|
2
Apply DP with Bitmask
Use dp[mask][i] to represent the minimum cost to visit all positions in the bitmask, ending at position i
3
Build Solution Incrementally
For each state, try extending to unvisited positions and update costs optimally
4
Complete the Cycle
Add the cost to return from the final position back to position 0 to complete the circular requirement
Key Takeaway
๐ŸŽฏ Key Insight: This TSP variant can be solved efficiently using DP with bitmasks, reducing complexity from O(n!) to O(nยฒ ร— 2โฟ) while guaranteeing the optimal solution.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ ร— 2โฟ)

For each of 2โฟ possible subsets and n positions, we try n transitions

n
2n
โš  Quadratic Growth
Space Complexity
O(n ร— 2โฟ)

DP table stores minimum cost for each (mask, position) pair

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 14
  • nums is a permutation of [0, 1, 2, ..., n - 1]
  • Time limit: 2 seconds per test case
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
27.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen