|
| 1 | +<p>You are given two integer arrays <code>nums</code> and <code>target</code>, each of length <code>n</code>, where <code>nums[i]</code> is the current value at index <code>i</code> and <code>target[i]</code> is the desired value at index <code>i</code>.</p> |
| 2 | + |
| 3 | +<p>You may perform the following operation any number of times (including zero):</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Choose an integer value <code>x</code></li> |
| 7 | + <li>Find all <strong>maximal contiguous segments</strong> where <code>nums[i] == x</code> (a segment is <strong>maximal</strong> if it cannot be extended to the left or right while keeping all values equal to <code>x</code>)</li> |
| 8 | + <li>For each such segment <code>[l, r]</code>, update <strong>simultaneously</strong>: |
| 9 | + <ul> |
| 10 | + <li><code>nums[l] = target[l], nums[l + 1] = target[l + 1], ..., nums[r] = target[r]</code></li> |
| 11 | + </ul> |
| 12 | + </li> |
| 13 | +</ul> |
| 14 | + |
| 15 | +<p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to <code>target</code>.</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | +<p><strong class="example">Example 1:</strong></p> |
| 19 | + |
| 20 | +<div class="example-block"> |
| 21 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], target = [2,1,3]</span></p> |
| 22 | + |
| 23 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 24 | + |
| 25 | +<p><strong>Explanation:</strong></p> |
| 26 | + |
| 27 | +<ul> |
| 28 | + <li>Choose <code>x = 1</code>: maximal segment <code>[0, 0]</code> updated -> nums becomes <code>[2, 2, 3]</code></li> |
| 29 | + <li>Choose <code>x = 2</code>: maximal segment <code>[0, 1]</code> updated (<code>nums[0]</code> stays 2, <code>nums[1]</code> becomes 1) -> <code>nums</code> becomes <code>[2, 1, 3]</code></li> |
| 30 | + <li>Thus, 2 operations are required to convert <code>nums</code> to <code>target</code>.</li> |
| 31 | +</ul> |
| 32 | +</div> |
| 33 | + |
| 34 | +<p><strong class="example">Example 2:</strong></p> |
| 35 | + |
| 36 | +<div class="example-block"> |
| 37 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,1,4], target = [5,1,4]</span></p> |
| 38 | + |
| 39 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 40 | + |
| 41 | +<p><strong>Explanation:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li>Choose <code>x = 4</code>: maximal segments <code>[0, 0]</code> and <code>[2, 2]</code> updated (<code>nums[2]</code> stays 4) -> <code>nums</code> becomes <code>[5, 1, 4]</code></li> |
| 45 | + <li>Thus, 1 operation is required to convert <code>nums</code> to <code>target</code>.</li> |
| 46 | +</ul> |
| 47 | +</div> |
| 48 | + |
| 49 | +<p><strong class="example">Example 3:</strong></p> |
| 50 | + |
| 51 | +<div class="example-block"> |
| 52 | +<p><strong>Input:</strong> <span class="example-io">nums = [7,3,7], target = [5,5,9]</span></p> |
| 53 | + |
| 54 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 55 | + |
| 56 | +<p><strong>Explanation:</strong></p> |
| 57 | + |
| 58 | +<ul> |
| 59 | + <li>Choose <code>x = 7</code>: maximal segments <code>[0, 0]</code> and <code>[2, 2]</code> updated -> <code>nums</code> becomes <code>[5, 3, 9]</code></li> |
| 60 | + <li>Choose <code>x = 3</code>: maximal segment <code>[1, 1]</code> updated -> <code>nums</code> becomes <code>[5, 5, 9]</code></li> |
| 61 | + <li>Thus, 2 operations are required to convert <code>nums</code> to <code>target</code>.</li> |
| 62 | +</ul> |
| 63 | +</div> |
| 64 | + |
| 65 | +<p> </p> |
| 66 | +<p><strong>Constraints:</strong></p> |
| 67 | + |
| 68 | +<ul> |
| 69 | + <li><code>1 <= n == nums.length == target.length <= 10<sup>5</sup></code></li> |
| 70 | + <li><code>1 <= nums[i], target[i] <= 10<sup>5</sup></code></li> |
| 71 | +</ul> |
0 commit comments