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)/使数组中位数等于 K 的最少操作数 [minimum-operations-to-make-median-of-array-equal-to-k].html
2024-04-07 13:02:43 +08:00

50 lines
2.3 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;和一个 <strong>非负</strong>&nbsp;整数&nbsp;<code>k</code>&nbsp;</p>
<p>一次操作中,你可以选择任一下标&nbsp;<code>i</code>&nbsp;,然后将&nbsp;<code>nums[i]</code>&nbsp;&nbsp;<code>1</code>&nbsp;或者减&nbsp;<code>1</code>&nbsp;</p>
<p>请你返回将 <code>nums</code>&nbsp;<strong>中位数</strong>&nbsp;变为 <code>k</code>&nbsp;所需要的 <strong>最少</strong>&nbsp;操作次数。</p>
<p>一个数组的 <strong>中位数</strong>&nbsp;指的是数组按 <strong>非递减</strong> 顺序排序后最中间的元素。如果数组长度为偶数,我们选择中间两个数的较大值为中位数。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [2,5,6,8,5], k = 4</span></p>
<p><span class="example-io"><b>输出:</b>2</span></p>
<p><b>解释:</b>我们将&nbsp;<code>nums[1]</code>&nbsp;<code>nums[4]</code>&nbsp;<code>1</code>&nbsp;得到&nbsp;<code>[2, 4, 6, 8, 4]</code>&nbsp;。现在数组的中位数等于&nbsp;<code>k</code>&nbsp;。所以答案为 2 。</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [2,5,6,8,5], k = 7</span></p>
<p><span class="example-io"><b>输出:</b>3</span></p>
<p><b>解释:</b>我们将&nbsp;<code>nums[1]</code>&nbsp;增加 1 两次,并且将&nbsp;<code>nums[2]</code>&nbsp;增加 1 一次,得到&nbsp;<code>[2, 7, 7, 8, 5]</code>&nbsp;。结果数组的中位数等于&nbsp;<code>k</code>&nbsp;。所以答案为 3 。</p>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [1,2,3,4,5,6], k = 4</span></p>
<p><span class="example-io"><b>输出:</b>0</span></p>
<p><b>解释:</b>数组中位数已经等于 <code>k</code>&nbsp;了,所以不需要进行任何操作。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</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>
</ul>