Insert and Find Kth Largest
GoogleOnsiteMedium
LargestFinder() starts with no numbers and keeps every number it is given, repeats included.
Part 1: Insert and find
Implement:
insert(num)addsnumto the collection.find_largest(k)returns the number at positionkwhen the whole collection is listed from largest to smallest, counting from 0. Sofind_largest(0)is the maximum, and a number that was inserted twice takes two positions.
find_largest is only called when the collection holds more than k numbers.
- Example 1
- init
[]- operations
[["insert",[3]],["insert",[3]],["insert",[2]],["find_largest",[0]],["find_largest",[1]],["find_largest",[2]]]- Output
[null,null,null,3,3,2]
Why: Inserting 3, 3, 2 gives the values 3, 3, 2 in descending order.
find_largest(0)is 3,find_largest(1)is the second 3 because duplicates count, andfind_largest(2)is 2.
Constraints
1 <= number of operations <= 10^5-10^9 <= num <= 10^90 <= k < number of values inserted so far
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.