← 返回 jpmorgan 的题目列表Keep Highest K Elements
类型:online_judge
Implement a class KeepHighestK that can store an arbitrary number of integers but only keep the top k largest elements. Support the following operations:
add(num: int) -> None: Add a number.
get_highest_k() -> List[int]: Return the current highest k elements in descending order.
Assume the interface can be called multiple times and aim for the lowest possible time complexity. Provide reasonable test cases.
Example
Input
3
1
4
2
add 3
add 1
add 4
add 2
get_highest_k