Question - Design a class/method AddAndGetTopK to add a number and get top K frequent elements.
Eg - k = 3
4
4,5
4,5,4 => 4,5
4,5,4,6 => 4,5,6
4,5,4,6,5 => 4,5,6
4,5,4,6,5,7 => 4,5,6
4,5,4,6,5,7,7 => 4,5,7
I suggessted a BST solution with O(logn) insertion time and O(logn) time for getting top K elements, by maintaining the count of nodes below every node.
Any other solutions?