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-cost-to-equalize-array].html
2024-05-16 15:32:41 +08:00

87 lines
4.2 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>给你一个整数数组&nbsp;<code>nums</code>&nbsp;和两个整数&nbsp;<code>cost1</code>&nbsp;<code>cost2</code>&nbsp;。你可以执行以下&nbsp;<strong>任一</strong>&nbsp;操作&nbsp;<strong>任意</strong>&nbsp;次:</p>
<ul>
<li><code>nums</code>&nbsp;中选择下标 <code>i</code>&nbsp;并且将 <code>nums[i]</code>&nbsp;<strong>增加</strong> <code>1</code>&nbsp;,开销为 <code>cost1</code></li>
<li>选择 <code>nums</code>&nbsp;中两个 <strong>不同</strong>&nbsp;下标 <code>i</code>&nbsp;<code>j</code>&nbsp;,并且将 <code>nums[i]</code>&nbsp;<code>nums[j]</code>&nbsp;&nbsp;<strong>增加</strong> <code>1</code>&nbsp;,开销为 <code>cost2</code>&nbsp;</li>
</ul>
<p>你的目标是使数组中所有元素都 <strong>相等</strong>&nbsp;,请你返回需要的 <strong>最小开销</strong>&nbsp;之和。</p>
<p>由于答案可能会很大,请你将它对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;后返回。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [4,1], cost1 = 5, cost2 = 2</span></p>
<p><span class="example-io"><b>输出:</b>15</span></p>
<p><strong>解释:</strong></p>
<p>执行以下操作可以使数组中所有元素相等:</p>
<ul>
<li>&nbsp;<code>nums[1]</code>&nbsp;增加 1 ,开销为 5 <code>nums</code> 变为&nbsp;<code>[4,2]</code>&nbsp;</li>
<li>&nbsp;<code>nums[1]</code>&nbsp;增加 1 ,开销为 5 <code>nums</code> 变为&nbsp;<code>[4,3]</code>&nbsp;</li>
<li>&nbsp;<code>nums[1]</code>&nbsp;增加 1 ,开销为 5 <code>nums</code> 变为&nbsp;<code>[4,4]</code>&nbsp;</li>
</ul>
<p>总开销为 15 。</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [2,3,3,3,5], cost1 = 2, cost2 = 1</span></p>
<p><span class="example-io"><b>输出:</b>6</span></p>
<p><b>解释:</b></p>
<p>执行以下操作可以使数组中所有元素相等:</p>
<ul>
<li>&nbsp;<code>nums[0]</code>&nbsp;<code>nums[1]</code>&nbsp;同时增加 1 ,开销为 1 <code>nums</code> 变为&nbsp;<code>[3,4,3,3,5]</code>&nbsp;</li>
<li>&nbsp;<code>nums[0]</code>&nbsp;<code>nums[2]</code>&nbsp;同时增加 1 ,开销为 1 <code>nums</code> 变为&nbsp;<code>[4,4,4,3,5]</code>&nbsp;</li>
<li>&nbsp;<code>nums[0]</code>&nbsp;<code>nums[3]</code>&nbsp;同时增加 1 ,开销为 1 <code>nums</code> 变为&nbsp;<code>[5,4,4,4,5]</code>&nbsp;</li>
<li>&nbsp;<code>nums[1]</code>&nbsp;<code>nums[2]</code>&nbsp;同时增加 1 ,开销为 1 <code>nums</code> 变为&nbsp;<code>[5,5,5,4,5]</code>&nbsp;</li>
<li>&nbsp;<code>nums[3]</code>&nbsp;增加 1 ,开销为 2 <code>nums</code> 变为&nbsp;<code>[5,5,5,5,5]</code>&nbsp;</li>
</ul>
<p>总开销为 6 。</p>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [3,5,3], cost1 = 1, cost2 = 3</span></p>
<p><span class="example-io"><b>输出:</b>4</span></p>
<p><strong>解释:</strong></p>
<p>执行以下操作可以使数组中所有元素相等:</p>
<ul>
<li>&nbsp;<code>nums[0]</code>&nbsp;增加 1 ,开销为 1 <code>nums</code> 变为&nbsp;<code>[4,5,3]</code>&nbsp;</li>
<li>&nbsp;<code>nums[0]</code>&nbsp;增加 1 ,开销为 1 <code>nums</code> 变为&nbsp;<code>[5,5,3]</code>&nbsp;</li>
<li>&nbsp;<code>nums[2]</code>&nbsp;增加 1 ,开销为 1 <code>nums</code> 变为&nbsp;<code>[5,5,4]</code>&nbsp;</li>
<li>&nbsp;<code>nums[2]</code>&nbsp;增加 1 ,开销为 1 <code>nums</code> 变为&nbsp;<code>[5,5,5]</code>&nbsp;</li>
</ul>
<p>总开销为 4 。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= cost1 &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= cost2 &lt;= 10<sup>6</sup></code></li>
</ul>