3991. Sort Array Using Prefix Reversals π
Description
You are given an integer array nums of length n, where nums is a permutation of the integers in the range [0, n - 1].
You are also given an integer array pre, where each pre[i] is a valid prefix length.
In one operation, you may choose any length x from pre and reverse the first x elements of nums.
For example, applying a prefix reversal of length 3 on [4, 1, 2, 3] results in [2, 1, 4, 3].
Return the minimum number of operations required to sort nums in ascending order. If it is impossible to sort nums, return -1.
Example 1:
Input: nums = [2,0,1], pre = [2,3]
Output: 2
Explanation:
- Reverse
pre[1] = 3elements to getnums = [1, 0, 2]. - Then reverse
pre[0] = 2elements to getnums = [0, 1, 2]. - Thus, the minimum number of prefix reversal required is 2.
Example 2:
Input: nums = [1,0,2], pre = [1,3]
Output: -1
Explanation:
It is impossible to sort the array using the given prefix lengths, so the answer is -1.
Example 3:
Input: nums = [0,1], pre = [2]
Output: 0
Explanation:
Since nums is already sorted, no prefix reversals are needed. Thus, the answer is 0.
Constraints:
1 <= n == nums.length <= 80 <= nums[i] <= n - 11 <= pre.length <= n1 <= pre[i] <= nβββββββnumsis a permutation of integers from 0 ton - 1.preconsists of unique integers.
Solutions
Solution 1: BFS
Thinking
\(n\le 8\) gives at most \(8!=40320\) permutations, and the allowed prefix flips are few, so BFS on the permutation is the minimum step count.
The target is \((0,1,\ldots,n-1)\). From the start tuple, reverse each prefix in \(\textit{pre}\); unvisited states are enqueued. Hitting the target is optimal; an empty queue is impossible.
Dedup with tuples or an base-\(8\) integer.
Since \(n \le 8\), the number of permutations is at most \(8! = 40320\), so we can use BFS to find the minimum number of operations.
Treat the current array as a state, and the target state is \([0, 1, \ldots, n - 1]\). If the initial state is already the target, return \(0\). Otherwise, start BFS from the initial state: each time take a state from the queue, enumerate every prefix length \(x\) in \(\textit{pre}\), and reverse the first \(x\) elements to obtain a new state. If the new state equals the target, return the current number of steps; otherwise, if it has not been visited, enqueue it. If the search finishes without reaching the target, return \(-1\).
For convenience of deduplication, encode each permutation as an integer in base \(8\) (every element lies in \([0, 7]\)).
The time complexity is \(O(n! \cdot m \cdot n)\), and the space complexity is \(O(n! \cdot n)\). Here, \(n\) is the length of the array, and \(m\) is the length of \(\textit{pre}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |