295. Find Median from Data Stream!
two PriorityQueue
follow-up question:
Follow up:
If all integer numbers from the stream are in the range
[0, 100]
, how would you optimize your solution?If
99%
of all integer numbers from the stream are in the range[0, 100]
, how would you optimize your solution?
Answer:
1
-we maintain HashMap -we also maintain total number of elements, so that median is just total-nums/2 th element from sorted array.
-then we go over Map from 1-100 and count element, when we hit total-nums/2 th element , that is our median
2.
add two variable to count the numbers that smaller and larger than 100 respectively.
Last updated