Minimum Reverse Operations Problem
Description
LeetCode Problem 2612.
You are given an integer n and an integer p representing an array arr of length n where all elements are set to 0's, except position p which is set to 1. You are also given an integer array banned containing restricted positions. Perform the following operation on arr:
- Reverse a subarray with size
kif the single 1 is not set to a position inbanned.
Return an integer array answer with n results where the i^th result is the minimum number of operations needed to bring the single 1 to position i in arr, or -1 if it is impossible.
Example 1:
Input: n = 4, p = 0, banned = [1,2], k = 4
Output: [0,-1,-1,1]
Explanation:
- Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0.
- We can never place 1 on the banned positions, so the answer for positions 1 and 2 is -1.
- Perform the operation of size 4 to reverse the whole array.
- After a single operation 1 is at position 3 so the answer for position 3 is 1.
Example 2:
Input: n = 5, p = 0, banned = [2,4], k = 3
Output: [0,-1,-1,-1,-1]
Explanation:
- Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0.
- We cannot perform the operation on the subarray positions [0, 2] because position 2 is in banned.
- Because 1 cannot be set at position 2, it is impossible to set 1 at other positions in more operations.
Example 3:
Input: n = 4, p = 2, banned = [0,1,3], k = 1
Output: [-1,-1,0,-1]
Explanation:
Perform operations of size 1 and 1 never changes its position.
Constraints:
1 <= n <= 10^50 <= p <= n - 10 <= banned.length <= n - 10 <= banned[i] <= n - 11 <= k <= nbanned[i] != p- all values in
bannedare unique
Solution
Approach: BFS + Union-Find over Unvisited Positions
Reversing a subarray [l, r] of length k that contains index i sends i to l + r - i. Sliding the window one step right adds 2 to that target, and one step left subtracts 2. So from i the reachable targets all have the same parity and form a run with step 2.
The window must stay inside the array, which bounds the targets: the smallest is mi = max(i - k + 1, k - i - 1) and the largest is mx = min(i + k - 1, 2n - k - i - 1). So from i we can go to mi, mi + 2, ..., mx, except to banned positions.
Run a BFS from p. Each position should be visited once, so we need to quickly find "the next unvisited position of the same parity that is at least x". Keep an array nxt where nxt[x] points to x if x is still unvisited and otherwise to x + 2, with path compression (a union-find). Banned positions and p start as visited. For each position i taken from the queue, repeatedly find the next unvisited position j in [mi, mx], set its answer to ans[i] + 1, mark it visited and push it. Positions never reached keep -1.
Complexity: O(n α(n)) time (nearly linear) and O(n) space, where n is the array length.
from collections import deque
class Solution:
def minReverseOperations(
self, n: int, p: int, banned: List[int], k: int
) -> List[int]:
# nxt[x]: the smallest unvisited index >= x with the same parity as x
nxt = list(range(n + 4))
def find(x: int) -> int:
root = x
while nxt[root] != root:
root = nxt[root]
while nxt[x] != root:
nxt[x], x = root, nxt[x]
return root
ans = [-1] * n
ans[p] = 0
nxt[p] = p + 2
for i in banned:
nxt[i] = i + 2
q = deque([p])
while q:
i = q.popleft()
mi = max(i - k + 1, k - i - 1)
mx = min(i + k - 1, n * 2 - k - i - 1)
j = find(mi)
while j <= mx:
ans[j] = ans[i] + 1
q.append(j)
nxt[j] = j + 2
j = find(j)
return ans