[LeetCode]#908. Smallest Range I
Environment: Python 3.8
Key technique: max
Given an array A
of integers, for each integer A[i]
we may choose any x
with -K <= x <= K
, and add x
to A[i]
.
After this process, we have some array B
.
Return the smallest possible difference between the maximum value of B
and the minimum value of B
.
Example 1:
Input: A = [1], K = 0
Output: 0
Explanation: B = [1]
Example 2:
Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]
Analysis:
- Use formula “max(max(A) — min(A) — 2 * K, 0)” to do this problem.
Solution:
class Solution:
def smallestRangeI(self, A: List[int], K: int) -> int:
return max(max(A) - min(A) - 2 * K, 0)
Submissions:

Reference: