This is an interview question. Design a class, which stores integers and provides two operations:
void insert(int k)
int getMedian()
I guess I can use BST so that insert
takes O(logN) and getMedian
takes O(logN) (for getMedian
I should add the number of of left/right children for each node).
Now I wonder if this is the most efficient solution and there is no better one.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…