3369. Design an Array Statistics Tracker π
DifficultyHard
Description
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 theStatisticsTrackerobject with an empty array.void addNumber(int number): Addnumberto 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.
Example 1:
Input:
["StatisticsTracker", "addNumber", "addNumber", "addNumber", "addNumber", "getMean", "getMedian", "getMode", "removeFirstAddedNumber", "getMode"]
[[], [4], [4], [2], [3], [], [], [], [], []]
Output:
[null, null, null, null, null, 3, 4, 4, null, 2]
Explanation
StatisticsTracker statisticsTracker = new StatisticsTracker();statisticsTracker.addNumber(4); // The data structure now contains [4]
statisticsTracker.addNumber(4); // The data structure now contains [4, 4]
statisticsTracker.addNumber(2); // The data structure now contains [4, 4, 2]
statisticsTracker.addNumber(3); // The data structure now contains [4, 4, 2, 3]
statisticsTracker.getMean(); // return 3
statisticsTracker.getMedian(); // return 4
statisticsTracker.getMode(); // return 4
statisticsTracker.removeFirstAddedNumber(); // The data structure now contains [4, 2, 3]
statisticsTracker.getMode(); // return 2
Example 2:
Input:
["StatisticsTracker", "addNumber", "addNumber", "getMean", "removeFirstAddedNumber", "addNumber", "addNumber", "removeFirstAddedNumber", "getMedian", "addNumber", "getMode"]
[[], [9], [5], [], [], [5], [6], [], [], [8], []]
Output:
[null, null, null, 7, null, null, null, null, 6, null, 5]
Explanation
StatisticsTracker statisticsTracker = new StatisticsTracker();statisticsTracker.addNumber(9); // The data structure now contains [9]
statisticsTracker.addNumber(5); // The data structure now contains [9, 5]
statisticsTracker.getMean(); // return 7
statisticsTracker.removeFirstAddedNumber(); // The data structure now contains [5]
statisticsTracker.addNumber(5); // The data structure now contains [5, 5]
statisticsTracker.addNumber(6); // The data structure now contains [5, 5, 6]
statisticsTracker.removeFirstAddedNumber(); // The data structure now contains [5, 6]
statisticsTracker.getMedian(); // return 6
statisticsTracker.addNumber(8); // The data structure now contains [5, 6, 8]
statisticsTracker.getMode(); // return 5
Constraints:
1 <= number <= 109- At most,
105calls will be made toaddNumber,removeFirstAddedNumber,getMean,getMedian, andgetModein total. removeFirstAddedNumber,getMean,getMedian, andgetModewill be called only if there is at least one element in the data structure.
Solutions
Solution 1: Queue + Hash Table + Ordered Set
Thinking
We must enqueue, drop the oldest value, and query mean, median, and mode in \(O(\log n)\) over \(10^5\) operations.
A queue keeps insertion order; \(s\) is the sum for the mean; \(sl\) is a sorted list for the median; another sorted list ordered by \((-\textit{cnt},\textit{value})\) yields the mode.
Frequency updates delete the old pair before inserting the new one so the mode set stays consistent.
We define a queue \(q\) to store the added numbers, a variable \(s\) to store the sum of all numbers, a hash table \(\textit{cnt}\) to store the occurrence count of each number, an ordered set \(sl\) to store all numbers, and an ordered set \(\textit{sl2}\) to store all numbers and their occurrence counts, sorted by occurrence count in descending order and by value in ascending order.
In the \(\textit{addNumber}\) method, we add the number to the queue \(q\), add the number to the ordered set \(sl\), then remove the number and its occurrence count from the ordered set \(\textit{sl2}\), update the occurrence count of the number, and finally add the number and its updated occurrence count to the ordered set \(\textit{sl2}\), and update the sum of all numbers. The time complexity is \(O(\log n)\).
In the \(\textit{removeFirstAddedNumber}\) method, we remove the earliest added number from the queue \(q\), remove the number from the ordered set \(sl\), then remove the number and its occurrence count from the ordered set \(\textit{sl2}\), update the occurrence count of the number, and finally add the number and its updated occurrence count to the ordered set \(\textit{sl2}\), and update the sum of all numbers. The time complexity is \(O(\log n)\).
In the \(\textit{getMean}\) method, we return the sum of all numbers divided by the number of numbers. The time complexity is \(O(1)\).
In the \(\textit{getMedian}\) method, we return the \(\textit{len}(q) / 2\)-th number in the ordered set \(sl\). The time complexity is \(O(1)\) or \(O(\log n)\).
In the \(\textit{getMode}\) method, we return the first number in the ordered set \(\textit{sl2}\). The time complexity is \(O(1)\).
In Python, we can directly access elements in an ordered set by index. In other languages, we can implement this using a heap.
The space complexity is \(O(n)\), where \(n\) is the number of added numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
1 | |