910-Smallest-Range-Ii

Sat 17 May 2025

https://leetcode.com/problems/smallest-range-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def smallestRangeII(self, A: List[int], K: int) -> int:
    A.sort()

    ans = A[-1] - A[0]
    left = A[0] + K
    right = A[-1] - K

    for a, b in zip(A, A[1:]):
      mini = min(left, b - K)
      maxi = max(right, a + K)
      ans = min(ans, maxi - mini)

    return ans
new Solution().smallestRangeII()

Score: 5

Category: leetcode