leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit ad755a99815ed18ccabb30ab2bbbfa32cb2487a2
parent 7476182e864c8d93f9da250789c163b7c1030b48
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sat, 24 Jan 2026 19:44:46 -0700

1984. minimum difference between highest and lowest of k scores

Diffstat:
Amin_diff_high_low_k_score.c | 28++++++++++++++++++++++++++++
Amin_diff_high_low_k_score.py | 25+++++++++++++++++++++++++
2 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/min_diff_high_low_k_score.c b/min_diff_high_low_k_score.c @@ -0,0 +1,28 @@ +// 1984. Minimum Difference Between Highest and Lowest of K Scores +#include "leetcode.h" + +/* + * you are given a 0 indexed integer array 'nums' where 'nums[i]' represents the + * score of the i'th student. you are also given an integer 'k'. pick the scores + * of any 'k' students from the array so that the difference betweenn the + * highest and the lowest of the 'k' scores is minimised. return the minimum + * possible difference. + */ + +int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } + +int minimumDifference(int *nums, int numsSize, int k) { + if (k == 1) + return 0; + qsort(nums, numsSize, sizeof(int), cmp); + int ans = INT_MAX; + for (int i = 0; i <= numsSize - k; i++) + ans = fmin(ans, nums[i + k - 1] - nums[i]); + return ans; +} + +int main() { + int n1[] = {90}, n2[] = {9, 4, 1, 7}; + printf("%d\n", minimumDifference(n1, ARRAY_SIZE(n1), 1)); // expect: 0 + printf("%d\n", minimumDifference(n2, ARRAY_SIZE(n2), 2)); // expect: 2 +} diff --git a/min_diff_high_low_k_score.py b/min_diff_high_low_k_score.py @@ -0,0 +1,25 @@ +# 1984. Minimum Difference Between Highest and Lowest of K Scores + +""" +you are given a 0 indexed integer array 'nums' where 'nums[i]' represents the +score of the i'th student. you are also given an integer 'k'. pick the scores +of any 'k' students from the array so that the difference betweenn the +highest and the lowest of the 'k' scores is minimised. return the minimum +possible difference. +""" + + +class Solution(object): + def minimumDifference(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: int + """ + return nums.sort() or min(r - l for l, r in zip(nums, nums[k - 1 :])) + + +if __name__ == "__main__": + obj = Solution() + print(obj.minimumDifference(nums=[90], k=1)) + print(obj.minimumDifference(nums=[9, 4, 1, 7], k=2))