LeetCode Solutions

908. Smallest Range I

Time:

Space:

			

class Solution {
 public:
  int smallestRangeI(vector<int>& A, int K) {
    int max = *max_element(begin(A), end(A));
    int min = *min_element(begin(A), end(A));

    return std::max(0, max - min - 2 * K);
  }
};
			

class Solution {
  public int smallestRangeI(int[] A, int K) {
    int max = Arrays.stream(A).max().getAsInt();
    int min = Arrays.stream(A).min().getAsInt();

    return Math.max(0, max - min - 2 * K);
  }
}
			

class Solution:
  def smallestRangeI(self, A: List[int], K: int) -> int:
    return max(0, max(A) - min(A) - 2 * K)