Bounded heap · Medium
A telemetry collector needs the kth largest amount after reading a complete batch. It cannot reorder the supplied input and should retain only the leading k candidates. The answer counts duplicate positions rather than distinct values and is missing when the requested rank does not exist.
Return the kth largest value with duplicates counted as separate positions while using at most O(k) additional storage.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[], k: integer }
Fields
- nums is the unsorted batch and k is a one-based descending rank.
Output
Return value
- Return a single numeric value or null when k < 1 or k exceeds the number of entries.
- This is one batch answer, not an array of prefix ranks. A value of zero or -1 is not an absence marker.
Constraints
- 0 <= nums.length <= 100000; values are signed integers between -1000000000 and 1000000000.
- -1 <= k <= 100001.
Examples
Example 1
{
"k": 2,
"nums": [
9,
9,
3
]
}Returns
9Example 2
{
"k": 3,
"nums": [
1,
4
]
}Returns
nullRequired complexity
O(n log(k + 1)) time and O(k) auxiliary space for valid k