Tap here to switch tabs
Problem
Submissions

Problem: Sort Array Using Prefix Reversals

Try to solve the Sort Array Using Prefix Reversals problem.

Statement

You are given an integer array nums of length nn, which is a permutation of integers from 00 to n−1n - 1, and an integer array pre containing unique integers.

In one operation, you can choose any integer kk from pre and reverse the prefix of nums of length kk (i.e., reverse the subarray from index 00 to index k−1k - 1).

Return the minimum number of operations required to sort nums in ascending order. If it is impossible to sort nums, return −1-1.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤8\leq 8

  • 0≤0 \leq nums[i] ≤n−1\leq n - 1

  • 1≤1 \leq pre.length ≤n\leq n

  • 1≤1 \leq pre[i] ≤n\leq n

  • nums is a permutation of integers from 00 to n−1n - 1.

  • pre consists of unique integers.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Sort Array Using Prefix Reversals

Try to solve the Sort Array Using Prefix Reversals problem.

Statement

You are given an integer array nums of length nn, which is a permutation of integers from 00 to n−1n - 1, and an integer array pre containing unique integers.

In one operation, you can choose any integer kk from pre and reverse the prefix of nums of length kk (i.e., reverse the subarray from index 00 to index k−1k - 1).

Return the minimum number of operations required to sort nums in ascending order. If it is impossible to sort nums, return −1-1.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤8\leq 8

  • 0≤0 \leq nums[i] ≤n−1\leq n - 1

  • 1≤1 \leq pre.length ≤n\leq n

  • 1≤1 \leq pre[i] ≤n\leq n

  • nums is a permutation of integers from 00 to n−1n - 1.

  • pre consists of unique integers.