Given an integer array nums representing a stream of numbers and an integer k, return an array where the i-th element is the k-th highest number seen in the stream after processing the first i + 1 elements of nums.
If fewer than k elements have been seen so far, the corresponding entry should be -1. Process the elements of nums in order, one at a time.
Function Signature
def kth_highest_in_stream(nums: list[int], k: int) -> list[int]:
Rules
-
The output has the same length as
nums
.
-
The
k
-th highest is the value at index
k - 1
when the elements seen so far are sorted in descending order.
-
Duplicates count individually: the
k
-th highest of
[4, 4, 4]
with
k = 3
is
4
.
-
-1
is the placeholder for positions where fewer than
k
elements have been seen. Values can be negative, so a real
k
-th highest of
-1
is also written as
-1
.
Constraints
-
1 <= k <= len(nums) <= 10^4
-
-10^4 <= nums[i] <= 10^4
Examples
Input: nums = [5,2,9,1,7,3], k = 2
Output: [-1,2,5,5,7,7]
After [5] only one element has been seen, so the first entry is -1. The later prefixes sorted in descending order are [5,2], [9,5,2], [9,5,2,1], [9,7,5,2,1] and [9,7,5,3,2,1], and their second-highest values are 2, 5, 5, 7 and 7.
Input: nums = [4,4,4,4], k = 3
Output: [-1,-1,4,4]
The first two prefixes have fewer than 3 elements. From the third element on, the three highest values are all 4, because duplicates count individually.