mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
42 lines
1.6 KiB
HTML
42 lines
1.6 KiB
HTML
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
|
|
|
|
<p>In one operation, you can choose any index <code>i</code> where <code>0 <= i < nums.length</code> and change <code>nums[i]</code> to <code>nums[i] + x</code> where <code>x</code> is an integer from the range <code>[-k, k]</code>. You can apply this operation <strong>at most once</strong> for each index <code>i</code>.</p>
|
|
|
|
<p>The <strong>score</strong> of <code>nums</code> is the difference between the maximum and minimum elements in <code>nums</code>.</p>
|
|
|
|
<p>Return <em>the minimum <strong>score</strong> of </em><code>nums</code><em> after applying the mentioned operation at most once for each index in it</em>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [1], k = 0
|
|
<strong>Output:</strong> 0
|
|
<strong>Explanation:</strong> The score is max(nums) - min(nums) = 1 - 1 = 0.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [0,10], k = 2
|
|
<strong>Output:</strong> 6
|
|
<strong>Explanation:</strong> Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [1,3,6], k = 3
|
|
<strong>Output:</strong> 0
|
|
<strong>Explanation:</strong> Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
|
<li><code>0 <= nums[i] <= 10<sup>4</sup></code></li>
|
|
<li><code>0 <= k <= 10<sup>4</sup></code></li>
|
|
</ul>
|