Class Solution

java.lang.Object
g0901_1000.s0910_smallest_range_ii.Solution

public class Solution extends Object
910 - Smallest Range II.<p>Medium</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>For each index <code>i</code> where <code>0 <= i < nums.length</code>, change <code>nums[i]</code> to be either <code>nums[i] + k</code> or <code>nums[i] - k</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 changing the values at each index</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1], k = 0</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The score is max(nums) - min(nums) = 1 - 1 = 0.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [0,10], k = 2</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,3,6], k = 3</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Change nums to be [4, 6, 3]. The score is max(nums) - min(nums) = 6 - 3 = 3.</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • smallestRangeII

      public int smallestRangeII(int[] nums, int k)