3810. Minimum Operations to Reach Target Array Medium
1/**
2 * [3810] Minimum Operations to Reach Target Array
3 *
4 * You are given two integer arrays nums and target, each of length n, where nums[i] is the current value at index i and target[i] is the desired value at index i.
5 * You may perform the following operation any number of times (including zero):
6 *
7 * Choose an integer value x
8 * Find all maximal contiguous segments where nums[i] == x (a segment is maximal if it cannot be extended to the left or right while keeping all values equal to x)
9 * For each such segment [l, r], update simultaneously:
10 *
11 * nums[l] = target[l], nums[l + 1] = target[l + 1], ..., nums[r] = target[r]
12 *
13 *
14 *
15 * Return the minimum number of operations required to make nums equal to target.
16 *
17 * <strong class="example">Example 1:
18 * <div class="example-block">
19 * Input: <span class="example-io">nums = [1,2,3], target = [2,1,3]</span>
20 * Output: <span class="example-io">2</span>
21 * Explanation:
22 *
23 * Choose x = 1: maximal segment [0, 0] updated -> nums becomes [2, 2, 3]
24 * Choose x = 2: maximal segment [0, 1] updated (nums[0] stays 2, nums[1] becomes 1) -> nums becomes [2, 1, 3]
25 * Thus, 2 operations are required to convert nums to target.
26 * </div>
27 * <strong class="example">Example 2:
28 * <div class="example-block">
29 * Input: <span class="example-io">nums = [4,1,4], target = [5,1,4]</span>
30 * Output: <span class="example-io">1</span>
31 * Explanation:
32 *
33 * Choose x = 4: maximal segments [0, 0] and [2, 2] updated (nums[2] stays 4) -> nums becomes [5, 1, 4]
34 * Thus, 1 operation is required to convert nums to target.
35 * </div>
36 * <strong class="example">Example 3:
37 * <div class="example-block">
38 * Input: <span class="example-io">nums = [7,3,7], target = [5,5,9]</span>
39 * Output: <span class="example-io">2</span>
40 * Explanation:
41 *
42 * Choose x = 7: maximal segments [0, 0] and [2, 2] updated -> nums becomes [5, 3, 9]
43 * Choose x = 3: maximal segment [1, 1] updated -> nums becomes [5, 5, 9]
44 * Thus, 2 operations are required to convert nums to target.
45 * </div>
46 *
47 * Constraints:
48 *
49 * 1 <= n == nums.length == target.length <= 10^5
50 * 1 <= nums[i], target[i] <= 10^5
51 *
52 */
53pub struct Solution {}
54
55// problem: https://leetcode.com/problems/minimum-operations-to-reach-target-array/
56// discuss: https://leetcode.com/problems/minimum-operations-to-reach-target-array/discuss/?currentPage=1&orderBy=most_votes&query=
57
58// submission codes start here
59
60impl Solution {
61 pub fn min_operations(nums: Vec<i32>, target: Vec<i32>) -> i32 {
62 0
63 }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_3810() {
74 }
75}
76Back
© 2026 bowen.ge All Rights Reserved.