Solution 1

  • TimeO(n log n)
  • SpaceO(1) (Assuming, sorted nums is not considered)

where, n is the length of nums
Solved in 4 mins 38 secs, all by yourself! Good job!

Python · 2026-01-25
class Solution:
    '''
    Time Complexity: O(n log n)
    Space Complexity: O(1) (Assuming, sorted nums is not considered)
    where, n is the length of nums
    Solved in 4 mins 38 secs, all by yourself! Good job!
    '''
    def minimumDifference(self, nums: List[int], k: int) -> int:
        nums.sort()
        output = nums[-1]-nums[0]
        k-=1
        for i in range(len(nums)-k):
            output = min(output, nums[i+k]-nums[i])
        return output
Leet Code/python.py · L4590–4604