<p>You are given an array <code>nums</code> which is a <span data-keyword="permutation">permutation</span> of <code>[0, 1, 2, ..., n - 1]</code>. The <strong>score</strong> of any permutation of <code>[0, 1, 2, ..., n - 1]</code> named <code>perm</code> is defined as:</p>

<p><code>score(perm) = |perm[0] - nums[perm[1]]| + |perm[1] - nums[perm[2]]| + ... + |perm[n - 1] - nums[perm[0]]|</code></p>

<p>Return the permutation <code>perm</code> which has the <strong>minimum</strong> possible score. If <em>multiple</em> permutations exist with this score, return the one that is <span data-keyword="lexicographically-smaller-array">lexicographically smallest</span> among them.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,0,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">[0,1,2]</span></p>

<p><strong>Explanation:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/04/04/example0gif.gif" style="width: 235px; height: 235px;" /></strong></p>

<p>The lexicographically smallest permutation with minimum cost is <code>[0,1,2]</code>. The cost of this permutation is <code>|0 - 0| + |1 - 2| + |2 - 1| = 2</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,2,1]</span></p>

<p><strong>Output:</strong> <span class="example-io">[0,2,1]</span></p>

<p><strong>Explanation:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/04/04/example1gif.gif" style="width: 235px; height: 235px;" /></strong></p>

<p>The lexicographically smallest permutation with minimum cost is <code>[0,2,1]</code>. The cost of this permutation is <code>|0 - 1| + |2 - 2| + |1 - 0| = 2</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n == nums.length &lt;= 14</code></li>
	<li><code>nums</code> is a permutation of <code>[0, 1, 2, ..., n - 1]</code>.</li>
</ul>

class Solution:

    def solve(self, nums: List[int],
                    perm: List[int], i: int, n: int, mask: int) -> int:
        if i == n: return abs(perm[n - 1] - nums[0])
        if mask in self.DP[i][perm[i - 1]]:
            return self.DP[i][perm[i - 1]][mask][0]
        minScore = n * n
        minNum = None
        for num in list(self.remaining):
            perm[i] = num
            self.remaining.remove(num)
            score = abs(perm[i - 1] - nums[perm[i]])
            score += self.solve(nums, perm, i + 1, n, mask - 2 ** num)
            self.remaining.add(num)
            if [score, num] < [minScore, minNum]: minScore, minNum = score, num
        self.DP[i][perm[i - 1]][mask] = [minScore, minNum]
        return minScore

    def findPermutation(self, nums: List[int]) -> List[int]:
        n = len(nums)
        perm = [0] * n
        mask = 2 ** n - 2
        self.DP = [[defaultdict(list) for _ in range(n)] for _ in range(n)]
        self.remaining = set(range(1, n))
        self.solve(nums, perm, 1, n, mask)
        for i in range(1, n):
            score, num = self.DP[i][perm[i - 1]][mask]
            perm[i] = num
            mask -= 2 ** perm[i]
        return perm
3223