Practice Minimum Reverse Operations
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
A length-n zero array has a single 1 at p. One operation reverses a contiguous length-k segment, provided the resulting position of the 1 is not in banned. Return the fewest operations needed to place the 1 at each index, independently from the initial state, or -1 for unreachable indices.
Source: Minimum Reverse Operations on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Input
n = 4, p = 0, banned = [1,2], k = 4
Output
[0,-1,-1,1]
# Example 2
Input
n = 5, p = 0, banned = [2,4], k = 3
Output
[0,-1,-1,-1,-1]
# Example 3
Input
n = 4, p = 2, banned = [0,1,3], k = 1
Output
[-1,-1,0,-1]
Constraints
1 <= n <= 10^5
0 <= p <= n - 1
0 <= banned.length <= n - 1
0 <= banned[i] <= n - 1
1 <= k <= n
banned[i] != p
all values in banned are unique
Related LeetCode Problems
Practice more problems similar to Minimum Reverse Operations.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.