1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/使数组等于目标数组所需的最少操作次数 [minimum-operations-to-make-array-equal-to-target].html
2024-08-06 08:46:50 +08:00

48 lines
1.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你两个长度相同的正整数数组 <code>nums</code><code>target</code></p>
<p>在一次操作中,你可以选择 <code>nums</code> 的任何<span data-keyword="subarray">子数组</span>,并将该子数组内的每个元素的值增加或减少 1。</p>
<p>返回使 <code>nums</code> 数组变为 <code>target</code> 数组所需的 <strong>最少 </strong>操作次数。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p>
<p><strong>输出:</strong> <span class="example-io">2</span></p>
<p><strong>解释:</strong></p>
<p>执行以下操作可以使 <code>nums</code> 等于 <code>target</code><br />
- <code>nums[0..3]</code> 增加 1<code>nums = [4,6,2,3]</code><br />
- <code>nums[3..3]</code> 增加 1<code>nums = [4,6,2,4]</code></p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p>
<p><strong>输出:</strong> <span class="example-io">5</span></p>
<p><strong>解释:</strong></p>
<p>执行以下操作可以使 <code>nums</code> 等于 <code>target</code><br />
- <code>nums[0..0]</code> 增加 1<code>nums = [2,3,2]</code><br />
- <code>nums[1..1]</code> 减少 1<code>nums = [2,2,2]</code><br />
- <code>nums[1..1]</code> 减少 1<code>nums = [2,1,2]</code><br />
- <code>nums[2..2]</code> 增加 1<code>nums = [2,1,3]</code><br />
- <code>nums[2..2]</code> 增加 1<code>nums = [2,1,4]</code></p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length == target.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i], target[i] &lt;= 10<sup>8</sup></code></li>
</ul>