Problem
Design a data structure that keeps track of the values in it and answers some queries regarding their mean, median, and mode.
Implement the StatisticsTracker
class.
StatisticsTracker()
: Initialize theStatisticsTracker
object with an empty array.void addNumber(int number)
: Addnumber
to the data structure.void removeFirstAddedNumber()
: Remove the earliest added number from the data structure.int getMean()
: Return the floored mean of the numbers in the data structure.int getMedian()
: Return the median of the numbers in the data structure.int getMode()
: Return the mode of the numbers in the data structure. If there are multiple modes, return the smallest one.
Note:
- The mean of an array is the sum of all the values divided by the number of values in the array.
- The median of an array is the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the larger of the two values is taken.
- The mode of an array is the element that appears most often in the array.
Examples
Example 1
|
|
Solution
Method 1 – Ordered Set, Hash Map, and Heaps for Efficient Statistics
Intuition
To efficiently support insert, erase, and various statistics queries (min, max, kth, count, sum, average, median, mode), we need data structures that allow fast insertion, deletion, and order statistics. An ordered set (like TreeSet or SortedList), hash maps for frequency, and heaps for mode/median can be combined to achieve this.
Approach
- Use a balanced BST or a sorted list (e.g.,
SortedList
in Python,TreeSet
in Java) to maintain the array in sorted order for fast min, max, kth, median, and count queries. - Use a hash map to track the frequency of each value for mode queries.
- Maintain a running sum for sum and average queries.
- For insert, add to the sorted list, update frequency map and sum.
- For erase, remove from the sorted list, update frequency map and sum.
- For min/max, return the first/last element in the sorted list.
- For kth, return the k-1 index in the sorted list.
- For count, use bisect to count elements less than or equal to a value.
- For sum/average, use the running sum and length.
- For median, use the middle element(s) in the sorted list.
- For mode, track the value(s) with the highest frequency.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(log n)
for insert/erase,O(1)
for min/max/sum/average/mode,O(log n)
for kth/count/median. - 🧺 Space complexity:
O(n)
, for storing the array and frequency map.