2612. Minimum Reverse Operations Hard

@problem@discussion
#Array#Breadth-First Search#Ordered Set



1/**
2 * [2612] Minimum Reverse Operations
3 *
4 * 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:
5 * 
6 * 	Reverse a <span data-keyword="subarray-nonempty">subarray</span> with size k if the single 1 is not set to a position in banned.
7 * 
8 * 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.
9 *  
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">n = 4, p = 0, banned = [1,2], k = 4</span>
13 * Output: <span class="example-io">[0,-1,-1,1]</span>
14 * Explanation:
15 * 
16 * 	Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0.
17 * 	We can never place 1 on the banned positions, so the answer for positions 1 and 2 is -1.
18 * 	Perform the operation of size 4 to reverse the whole array.
19 * 	After a single operation 1 is at position 3 so the answer for position 3 is 1.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">n = 5, p = 0, banned = [2,4], k = 3</span>
24 * Output: <span class="example-io">[0,-1,-1,-1,-1]</span>
25 * Explanation:
26 * 
27 * 	Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0.
28 * 	We cannot perform the operation on the subarray positions [0, 2] because position 2 is in banned.
29 * 	Because 1 cannot be set at position 2, it is impossible to set 1 at other positions in more operations.
30 * </div>
31 * <strong class="example">Example 3:
32 * <div class="example-block">
33 * Input: <span class="example-io">n = 4, p = 2, banned = [0,1,3], k = 1</span>
34 * Output: <span class="example-io">[-1,-1,0,-1]</span>
35 * Explanation:
36 * Perform operations of size 1 and 1 never changes its position.
37 * </div>
38 *  
39 * Constraints:
40 * 
41 * 	1 <= n <= 10^5
42 * 	0 <= p <= n - 1
43 * 	0 <= banned.length <= n - 1
44 * 	0 <= banned[i] <= n - 1
45 * 	1 <= k <= n 
46 * 	banned[i] != p
47 * 	all values in banned are unique 
48 * 
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/minimum-reverse-operations/
53// discuss: https://leetcode.com/problems/minimum-reverse-operations/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58    pub fn min_reverse_operations(n: i32, p: i32, banned: Vec<i32>, k: i32) -> Vec<i32> {
59        vec![]
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_2612() {
71    }
72}
73


Back
© 2025 bowen.ge All Rights Reserved.