mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-11-12 15:25:48 +08:00
update
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
<p data-end="180" data-start="93">You are given two integer arrays <code>nums1</code> of length <code>n</code> and <code>nums2</code> of length <code>n + 1</code>.</p>
|
||||
|
||||
<p>You want to transform <code>nums1</code> into <code>nums2</code> using the <strong>minimum</strong> number of operations.</p>
|
||||
|
||||
<p>You may perform the following operations <strong>any</strong> number of times, each time choosing an index <code>i</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Increase</strong> <code>nums1[i]</code> by 1.</li>
|
||||
<li><strong>Decrease</strong> <code>nums1[i]</code> by 1.</li>
|
||||
<li><strong>Append</strong> <code>nums1[i]</code> to the <strong>end</strong> of the array.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of operations required to transform <code>nums1</code> into <code>nums2</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums1 = [2,8], nums2 = [1,7,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th align="center" style="border: 1px solid black;">Step</th>
|
||||
<th align="center" style="border: 1px solid black;"><code>i</code></th>
|
||||
<th align="center" style="border: 1px solid black;">Operation</th>
|
||||
<th align="center" style="border: 1px solid black;"><code>nums1[i]</code></th>
|
||||
<th align="center" style="border: 1px solid black;">Updated <code>nums1</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">Append</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 8, 2]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">Decrement</td>
|
||||
<td align="center" style="border: 1px solid black;">Decreases to 1</td>
|
||||
<td align="center" style="border: 1px solid black;">[1, 8, 2]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">3</td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">Decrement</td>
|
||||
<td align="center" style="border: 1px solid black;">Decreases to 7</td>
|
||||
<td align="center" style="border: 1px solid black;">[1, 7, 2]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">4</td>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">Increment</td>
|
||||
<td align="center" style="border: 1px solid black;">Increases to 3</td>
|
||||
<td align="center" style="border: 1px solid black;">[1, 7, 3]</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, after 4 operations <code>nums1</code> is transformed into <code>nums2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums1 = [1,3,6], nums2 = [2,4,5,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th align="center" style="border: 1px solid black;">Step</th>
|
||||
<th align="center" style="border: 1px solid black;"><code>i</code></th>
|
||||
<th align="center" style="border: 1px solid black;">Operation</th>
|
||||
<th align="center" style="border: 1px solid black;"><code>nums1[i]</code></th>
|
||||
<th align="center" style="border: 1px solid black;">Updated <code>nums1</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">Append</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">[1, 3, 6, 3]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">Increment</td>
|
||||
<td align="center" style="border: 1px solid black;">Increases to 2</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 3, 6, 3]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">3</td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">Increment</td>
|
||||
<td align="center" style="border: 1px solid black;">Increases to 4</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 4, 6, 3]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">4</td>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">Decrement</td>
|
||||
<td align="center" style="border: 1px solid black;">Decreases to 5</td>
|
||||
<td align="center" style="border: 1px solid black;">[2, 4, 5, 3]</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, after 4 operations <code>nums1</code> is transformed into <code>nums2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums1 = [2], nums2 = [3,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th align="center" style="border: 1px solid black;">Step</th>
|
||||
<th align="center" style="border: 1px solid black;"><code>i</code></th>
|
||||
<th align="center" style="border: 1px solid black;">Operation</th>
|
||||
<th align="center" style="border: 1px solid black;"><code>nums1[i]</code></th>
|
||||
<th align="center" style="border: 1px solid black;">Updated <code>nums1</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">Increment</td>
|
||||
<td align="center" style="border: 1px solid black;">Increases to 3</td>
|
||||
<td align="center" style="border: 1px solid black;">[3]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">2</td>
|
||||
<td align="center" style="border: 1px solid black;">0</td>
|
||||
<td align="center" style="border: 1px solid black;">Append</td>
|
||||
<td align="center" style="border: 1px solid black;">-</td>
|
||||
<td align="center" style="border: 1px solid black;">[3, 3]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" style="border: 1px solid black;">3</td>
|
||||
<td align="center" style="border: 1px solid black;">1</td>
|
||||
<td align="center" style="border: 1px solid black;">Increment</td>
|
||||
<td align="center" style="border: 1px solid black;">Increases to 4</td>
|
||||
<td align="center" style="border: 1px solid black;">[3, 4]</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, after 3 operations <code>nums1</code> is transformed into <code>nums2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums2.length == n + 1</code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
Reference in New Issue
Block a user