Minimum difference between highest and lowest of k scores
October 2, 2022
sliding-windowProblem URL: Minimum difference between highest and lowest of k scores
We will first sort the elements and then loop over all the elements and take the difference between k elements, and keep a running minimum and then return that as result.
class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
k -= 1
minDiff = math.inf
for i in range(len(nums)-k):
minDiff = min(minDiff, nums[i+k] - nums[i])
return minDiff
Time Complexity: O(nlog(n))
Space Complexity: O(1)