1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-12-19 18:34:59 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/使数组元素相等的最小操作次数(English) [minimum-operations-to-equalize-subarrays].html
2025-12-06 16:04:11 +08:00

129 lines
5.3 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can <strong>increase or decrease </strong>any element of <code>nums</code> by <strong>exactly</strong> <code>k</code>.</p>
<p>You are also given a 2D integer array <code>queries</code>, where each <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, find the <strong>minimum</strong> number of operations required to make <strong>all</strong> elements in the <strong><span data-keyword="subarray-nonempty">subarray</span></strong> <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code> <strong>equal</strong>. If it is impossible, the answer for that query is <code>-1</code>.</p>
<p>Return an array <code>ans</code>, where <code>ans[i]</code> is the answer for the <code>i<sup>th</sup></code> query.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,7], k = 3, queries = [[0,1],[0,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal set of operations:</p>
<table style="border: 1px solid black;">
<tbody>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>[l<sub>i</sub>, r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;"><code>nums[l<sub>i</sub>..r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;">Possibility</th>
<th style="border: 1px solid black;">Operations</th>
<th style="border: 1px solid black;">Final<br />
<code>nums[l<sub>i</sub>..r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;"><code>ans[i]</code></th>
</tr>
</tbody>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">[1, 4]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;"><code>nums[0] + k = 1 + 3 = 4 = nums[1]</code></td>
<td style="border: 1px solid black;">[4, 4]</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[0, 2]</td>
<td style="border: 1px solid black;">[1, 4, 7]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;"><code>nums[0] + k = 1 + 3 = 4 = nums[1]<br />
nums[2] - k = 7 - 3 = 4 = nums[1]</code></td>
<td style="border: 1px solid black;">[4, 4, 4]</td>
<td style="border: 1px solid black;">2</td>
</tr>
</tbody>
</table>
<p>Thus, <code>ans = [1, 2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4], k = 2, queries = [[0,2],[0,0],[1,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,0,1]</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal set of operations:</p>
<table style="border: 1px solid black;">
<tbody>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>[l<sub>i</sub>, r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;"><code>nums[l<sub>i</sub>..r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;">Possibility</th>
<th style="border: 1px solid black;">Operations</th>
<th style="border: 1px solid black;">Final<br />
<code>nums[l<sub>i</sub>..r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;"><code>ans[i]</code></th>
</tr>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[0, 2]</td>
<td style="border: 1px solid black;">[1, 2, 4]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">-</td>
<td style="border: 1px solid black;">[1, 2, 4]</td>
<td style="border: 1px solid black;">-1</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[0, 0]</td>
<td style="border: 1px solid black;">[1]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;">Already equal</td>
<td style="border: 1px solid black;">[1]</td>
<td style="border: 1px solid black;">0</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">[1, 2]</td>
<td style="border: 1px solid black;">[2, 4]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;"><code>nums[1] + k = 2 + 2 = 4 = nums[2]</code></td>
<td style="border: 1px solid black;">[4, 4]</td>
<td style="border: 1px solid black;">1</td>
</tr>
</tbody>
</table>
<p>Thus, <code>ans = [-1, 0, 1]</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 4 &times; 10<sup>4</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 4 &times; 10<sup>4</sup></code></li>
<li><code><sup></sup>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li>
</ul>