#908

Smallest Range I

pupil · 320 · lc easy +21 · verified · 73.3% accepted · 787 likes · top 83%

Description

Given an integer array nums and an integer k, you may independently shift each element by any amount in [-k, k]. After all adjustments, what is the smallest achievable difference between the largest and smallest values in the array?

Example 1:

Input: nums = [1], k = 0
Output: 0
Explanation: The score is max(nums) - min(nums) = 1 - 1 = 0.

Example 2:

Input: nums = [0,10], k = 2
Output: 6
Explanation: Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.

Example 3:

Input: nums = [1,3,6], k = 3
Output: 0
Explanation: Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.

Code

1
2
3