Watch 5 video solutions for Find the Minimum Cost Array Permutation, a hard level problem involving Array, Dynamic Programming, Bit Manipulation. This walkthrough by codestorywithMIK has 7,167 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an array nums which is a permutation of [0, 1, 2, ..., n - 1]. The score of any permutation of [0, 1, 2, ..., n - 1] named perm is defined as:
score(perm) = |perm[0] - nums[perm[1]]| + |perm[1] - nums[perm[2]]| + ... + |perm[n - 1] - nums[perm[0]]|
Return the permutation perm which has the minimum possible score. If multiple permutations exist with this score, return the one that is lexicographically smallest among them.
Example 1:
Input: nums = [1,0,2]
Output: [0,1,2]
Explanation:

The lexicographically smallest permutation with minimum cost is [0,1,2]. The cost of this permutation is |0 - 0| + |1 - 2| + |2 - 1| = 2.
Example 2:
Input: nums = [0,2,1]
Output: [0,2,1]
Explanation:

The lexicographically smallest permutation with minimum cost is [0,2,1]. The cost of this permutation is |0 - 1| + |2 - 2| + |1 - 0| = 2.
Constraints:
2 <= n == nums.length <= 14nums is a permutation of [0, 1, 2, ..., n - 1].Problem Overview: You are given an array and must build a permutation that minimizes the total cost defined by the relationship between adjacent elements. The challenge is deciding the order of elements so that the cumulative transition cost stays as small as possible. Since permutations grow factorially, naive solutions quickly become infeasible.
Approach 1: Brute Force with Permutations (O(n! * n) time, O(n) space)
The most direct strategy is to generate every possible permutation of the array and compute the total cost for each ordering. Use a permutation generator (such as backtracking or Python's itertools.permutations) and iterate through the resulting order to accumulate adjacent costs. Track the minimum cost encountered. This approach is easy to implement and helps verify correctness for small inputs, but the factorial growth makes it impractical beyond small n. It mainly serves as a baseline to understand the search space.
Approach 2: Brute Force with State Tracking (O(n^2 * 2^n) time, O(n * 2^n) space)
A better strategy models the problem as visiting elements in different orders using bitmask states. Represent the set of used indices with a bitmask and store the minimum cost for reaching a state ending at a specific element. Transition by iterating over unused elements and updating the cost based on the previous element. This reduces redundant work compared to pure permutation generation and leverages bitmask state compression with dynamic programming. The idea is similar to a Traveling Salesman–style DP where the state is (mask, last).
Approach 3: Greedy Approach with Pairings (O(n log n) time, O(n) space)
If the cost function depends on relative differences between elements, sorting the array and pairing elements strategically can significantly reduce total transitions. Start by sorting the input and greedily placing elements so that adjacent differences stay small. The algorithm iterates through the sorted list and constructs a permutation that avoids large jumps between neighbors. This approach works well when the cost behaves monotonically with element distance, and it relies heavily on properties of the array values.
Approach 4: Greedy with Lexicographical Construction (O(n log n) time, O(n) space)
This variant builds the permutation incrementally while maintaining the smallest achievable cost prefix. Use sorted candidates and pick the next element that preserves the minimal cost pattern while keeping the permutation lexicographically valid. The algorithm repeatedly selects the best remaining element using comparisons against the last chosen value. Although greedy decisions guide the process, careful ordering ensures the global cost remains minimal.
Recommended for interviews: Start by describing the permutation brute force solution to show understanding of the search space. Then move to the bitmask dynamic programming formulation, which reduces repeated computation and demonstrates strong problem‑solving ability. Interviewers usually expect recognition of the (mask, last) DP state and its O(n^2 * 2^n) complexity, which is the scalable solution for moderate input sizes.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force with Permutations | O(n! * n) | O(n) | Very small arrays or verifying correctness |
| Brute Force with Bitmask DP | O(n^2 * 2^n) | O(n * 2^n) | General case when exploring permutations efficiently |
| Greedy Pairing Strategy | O(n log n) | O(n) | When cost correlates with value differences |
| Greedy Lexicographical Construction | O(n log n) | O(n) | When maintaining minimal prefix cost while building permutation |