2022-03-27 18:27:43 +08:00
< p > You are given a < strong > 0-indexed< / strong > integer array < code > nums< / code > , where < code > nums[i]< / code > represents the score of the < code > i< sup > th< / sup > < / code > student. You are also given an integer < code > k< / code > .< / p >
< p > Pick the scores of any < code > k< / code > students from the array so that the < strong > difference< / strong > between the < strong > highest< / strong > and the < strong > lowest< / strong > of the < code > k< / code > scores is < strong > minimized< / strong > .< / p >
< p > Return < em > the < strong > minimum< / strong > possible difference< / em > .< / p >
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 18:27:43 +08:00
< pre >
< strong > Input:< / strong > nums = [90], k = 1
< strong > Output:< / strong > 0
< strong > Explanation:< / strong > There is one way to pick score(s) of one student:
- [< strong > < u > 90< / u > < / strong > ]. The difference between the highest and lowest score is 90 - 90 = 0.
The minimum possible difference is 0.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 18:27:43 +08:00
< pre >
< strong > Input:< / strong > nums = [9,4,1,7], k = 2
< strong > Output:< / strong > 2
< strong > Explanation:< / strong > There are six ways to pick score(s) of two students:
- [< strong > < u > 9< / u > < / strong > ,< strong > < u > 4< / u > < / strong > ,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.
- [< strong > < u > 9< / u > < / strong > ,4,< strong > < u > 1< / u > < / strong > ,7]. The difference between the highest and lowest score is 9 - 1 = 8.
- [< strong > < u > 9< / u > < / strong > ,4,1,< strong > < u > 7< / u > < / strong > ]. The difference between the highest and lowest score is 9 - 7 = 2.
- [9,< strong > < u > 4< / u > < / strong > ,< strong > < u > 1< / u > < / strong > ,7]. The difference between the highest and lowest score is 4 - 1 = 3.
- [9,< strong > < u > 4< / u > < / strong > ,1,< strong > < u > 7< / u > < / strong > ]. The difference between the highest and lowest score is 7 - 4 = 3.
- [9,4,< strong > < u > 1< / u > < / strong > ,< strong > < u > 7< / u > < / strong > ]. The difference between the highest and lowest score is 7 - 1 = 6.
The minimum possible difference is 2.< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = k < = nums.length < = 1000< / code > < / li >
< li > < code > 0 < = nums[i] < = 10< sup > 5< / sup > < / code > < / li >
< / ul >