Tap here to switch tabs
Problem
Submissions

Problem: Minimum Reverse Operations

Try to solve the Minimum Reverse Operations problem.

Statement

You are given an integer n and an integer p, representing the initial index of a target element in an array of size n with indices from 00 to n −1- 1.

You are also given an integer array banned containing indices that the target element can never occupy, and an integer k.

In one operation, you can select any contiguous subarray of length k and reverse it. If the target element is within this subarray, its position changes accordingly.

Return an array ans of length n, where ans[i] is the minimum number of reverse operations needed to move the target element from index p to index i, or −1-1 if index i is unreachable.

Constraints:

  • 1≤1 \leq n ≤105\leq 10^5

  • 0≤0 \leq p ≤\leq n −1- 1

  • 0≤0 \leq banned.length ≤\leq n −1- 1

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

  • 1≤1 \leq k ≤\leq n

  • banned[i] ≠\neq p

  • All values in banned are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Reverse Operations

Try to solve the Minimum Reverse Operations problem.

Statement

You are given an integer n and an integer p, representing the initial index of a target element in an array of size n with indices from 00 to n −1- 1.

You are also given an integer array banned containing indices that the target element can never occupy, and an integer k.

In one operation, you can select any contiguous subarray of length k and reverse it. If the target element is within this subarray, its position changes accordingly.

Return an array ans of length n, where ans[i] is the minimum number of reverse operations needed to move the target element from index p to index i, or −1-1 if index i is unreachable.

Constraints:

  • 1≤1 \leq n ≤105\leq 10^5

  • 0≤0 \leq p ≤\leq n −1- 1

  • 0≤0 \leq banned.length ≤\leq n −1- 1

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

  • 1≤1 \leq k ≤\leq n

  • banned[i] ≠\neq p

  • All values in banned are unique.