Classic problem #1984EasyArraySliding WindowSorting
Minimum Difference Between Highest and Lowest of K Scores
Real-world version: The Tightest Choice of k ReadingsTracked in this browser only
Read Minimum Difference Between Highest and Lowest of K Scores on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
A rack holds the readings readings. Choose exactly k of them, and let the spread of a choice be its largest reading less its smallest.
Return the smallest spread any choice of k readings can manage.
Examples
Example 1
- Input
- readings = [1, 5, 6, 14, 15], k = 3
- Output
- 5
In order the readings run 1, 5, 6, 14, 15. The three standing closest together are 1, 5 and 6, spreading 5.
Example 2
- Input
- readings = [5, 5, 5], k = 2
- Output
- 0
Every reading is the same, so any two of them spread nothing at all.
Example 3
- Input
- readings = [1, 100000], k = 2
- Output
- 99999
Both readings have to be taken, so the spread is the gap between them.
Constraints
- 1 <= k <= readings.length <= 1000
- 0 <= readings[i] <= 10^5
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def minimum_difference(readings: list[int], k: int) -> int:Java
public int minimumDifference(int[] readings, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Longest Harmonious SubsequenceReal-world version: Widest One-Degree SampleEasy
- Frequency of the Most Frequent ElementReal-world version: Plates at One ThicknessMedium
- Find K Closest ElementsReal-world version: Nearest Gauge MarksMedium
- Maximum Coins From K Consecutive BagsReal-world version: Richest Run of Consecutive BinsMedium