Find the Kth Highest Reading in a Sensor Batch
Implement kthHighestReading
A batch of sensor readings arrives as an unordered list. Report the k-th highest reading — the value that would sit in position k if the readings were sorted from highest to lowest. Repeated readings occupy separate positions, so a reading that appears twice can hold two ranks.
Sorting the whole batch answers the question but orders far more than is needed. A min-heap capped at k entries keeps exactly the k highest readings seen so far, with the k-th highest always sitting at its root.
Example 1:
Input: readings = [7,2,9,4,11], k = 2
Output: 9
Example 2:
Input: readings = [6,6,2,8,8,1], k = 3
Output: 6
Example 3:
Input: readings = [-4,-9,-1], k = 3
Output: -9
+ 10 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ k ≤ readings.length ≤ 100 - ●
-1000 ≤ readings[i] ≤ 1000 - ●
Readings may repeat; a repeated value counts once for every time it appears when ranking (the ranking is by position in the sorted order, not by distinct values)
readings =
[7, 2, 9, 4, 11]
k =
2