A quiz app shows a live leaderboard. New scores keep arriving, and a score can be negative. The leaderboard must always know which score sits at rank k when all scores are sorted from the highest to the lowest. Equal scores each take a rank of their own.
Write a class KthLargest with these calls:
KthLargest(int k, int[] nums) starts a leaderboard that tracks rank k. The scores in nums are already on the leaderboard.
int add(int val) puts the new score val on the leaderboard and returns the score at rank k after that.
Examples
Example 1:
Input
["KthLargest","add","add","add","add"]
[[2,[7,3,9]],[5],[10],[1],[11]]
Output
[null,7,9,9,10]
Explanation
The leaderboard starts with 9, 7 and 3, and k = 2.
add(5): the leaderboard is 9, 7, 5, 3. Rank 2 is 7.
add(10): the leaderboard is 10, 9, 7, 5, 3. Rank 2 is 9.
add(1): the new score is too low to matter, so rank 2 is still 9.
add(11): the leaderboard starts 11, 10, 9, and rank 2 is 10.
Example 2:
Input
["KthLargest","add","add","add"]
[[1,[]],[4],[2],[6]]
Output
[null,4,4,6]
Explanation
With k = 1, the answer is simply the top score so far: 4, then 4 again, then 6.