2612. Minimum Reverse Operations
You are given an integer
nand an integerpin the range[0, n - 1]. Representing a 0-indexed arrayarrof lengthnwhere all positions are set to0‘s, except positionpwhich is set to1.You are also given an integer array
bannedcontaining some positions from the array. For the i**th** position inbanned,arr[banned[i]] = 0, andbanned[i] != p.You can perform multiple operations on
arr. In an operation, you can choose a subarray with sizekand reverse the subarray. However, the1inarrshould never go to any of the positions inbanned. In other words, after each operationarr[banned[i]]remains0.Return an array
answhere for eachifrom[0, n - 1],ans[i]is the minimum number of reverse operations needed to bring the1to positioniin arr, or-1if it is impossible.
- A subarray is a contiguous non-empty sequence of elements within an array.
- The values of
ans[i]are independent for alli‘s.- The reverse of an array is an array containing the values in reverse order.
1 | class Solution { |