LeetCode 1984: Minimum Difference Between Highest and Lowest of K Scores Solution
Master LeetCode problem 1984 (Minimum Difference Between Highest and Lowest of K Scores), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1984. Minimum Difference Between Highest and Lowest of K Scores
Problem Explanation
Explanation
To minimize the difference between the highest and lowest scores of any k students, we can sort the scores in non-decreasing order. Then, we iterate through the sorted scores and calculate the difference between the score at index i+k-1 and the score at index i. We keep track of the minimum difference found during this process.
Time complexity: O(nlogn) where n is the number of scores
Space complexity: O(1)
Solution Code
import java.util.Arrays;
class Solution {
public int minDifference(int[] nums, int k) {
Arrays.sort(nums);
int minDiff = Integer.MAX_VALUE;
for (int i = 0; i <= nums.length - k; i++) {
minDiff = Math.min(minDiff, nums[i + k - 1] - nums[i]);
}
return minDiff;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1984 (Minimum Difference Between Highest and Lowest of K Scores)?
This page provides optimized solutions for LeetCode problem 1984 (Minimum Difference Between Highest and Lowest of K Scores) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1984 (Minimum Difference Between Highest and Lowest of K Scores)?
The time complexity for LeetCode 1984 (Minimum Difference Between Highest and Lowest of K Scores) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1984 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1984 in Java, C++, or Python.