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 k if the single 1 is not set to a position in banned.
Return an integer array answer with n results where the ith result is __ the minimum number of operations needed to bring the single 1 to position
i in arr, or -1 if it is impossible.
Input: n =4, p =0, banned =[1,2], k =4Output: [0,-1,-1,1]Explanation:
* Initially 1is placed at position 0 so the number of operations we need for position 0is0.* We can never place 1 on the banned positions, so the answer for positions 1 and 2is-1.* Perform the operation of size 4 to reverse the whole array.* After a single operation 1is at position 3 so the answer for position 3is1.
Input: n =5, p =0, banned =[2,4], k =3Output: [0,-1,-1,-1,-1]Explanation:
* Initially 1is placed at position 0 so the number of operations we need for position 0is0.* We cannot perform the operation on the subarray positions `[0, 2]` because position 2isin banned.* Because 1 cannot be set at position 2, it is impossible to set 1 at other positions in more operations.
Model the problem as a shortest path search: from position p, use BFS to reach all positions, only moving to positions not banned. For each position, compute all possible next positions by reversing a subarray of size k.
import java.util.*;
classSolution {
publicint[]minReverseOperations(int n, int p, int[] banned, int k) {
int[] res =newint[n]; Arrays.fill(res,-1);
boolean[] isBanned =newboolean[n];
for (int b : banned) isBanned[b]=true;
Queue<Integer> q =new LinkedList<>(); q.add(p); res[p]=0;
while (!q.isEmpty()) {
int u = q.poll();
int minL = Math.max(u-k+1,0), maxL = Math.min(u,n-k);
for (int l = minL; l <= maxL; l++) {
int v = l+k-1-(u-l);
if (!isBanned[v]&& res[v]==-1) {
res[v]=res[u]+1; q.add(v);
}
}
}
return res;
}
}
classSolution {
funminReverseOperations(n: Int, p: Int, banned: IntArray, k: Int): IntArray {
val res = IntArray(n){-1}
val isBanned = BooleanArray(n)
for (b in banned) isBanned[b]=trueval q = ArrayDeque<Int>()
q.add(p); res[p]=0while (q.isNotEmpty()) {
val u = q.removeFirst()
val minL = maxOf(u-k+1,0); val maxL = minOf(u,n-k)
for (l in minL..maxL) {
val v = l+k-1-(u-l)
if (!isBanned[v] && res[v]==-1) {
res[v]=res[u]+1; q.add(v)
}
}
}
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from collections import deque
classSolution:
defminReverseOperations(self, n: int, p: int, banned: list[int], k: int) -> list[int]:
res = [-1]*n
is_banned = [False]*n
for b in banned: is_banned[b]=True q = deque([p]); res[p]=0while q:
u = q.popleft()
minL, maxL = max(u-k+1,0), min(u,n-k)
for l in range(minL, maxL+1):
v = l+k-1-(u-l)
ifnot is_banned[v] and res[v]==-1:
res[v]=res[u]+1; q.append(v)
return res
use std::collections::VecDeque;
impl Solution {
pubfnmin_reverse_operations(n: i32, p: i32, banned: Vec<i32>, k: i32) -> Vec<i32> {
let n = n asusize; let p = p asusize; let k = k asusize;
letmut res =vec![-1;n]; letmut is_banned =vec![false;n];
for b in banned { is_banned[b asusize]=true; }
letmut q = VecDeque::new(); q.push_back(p); res[p]=0;
whilelet Some(u) = q.pop_front() {
let min_l = u+1>=k ? u+1-k : 0;
let max_l =if u>n-k { n-k } else { u };
for l in min_l..=max_l {
let v = l+k-1-(u-l);
if v<n &&!is_banned[v] && res[v]==-1 {
res[v]=res[u]+1; q.push_back(v);
}
}
}
res
}
}