-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3369_design_an_array_statistics_tracker.py
58 lines (48 loc) · 1.71 KB
/
3369_design_an_array_statistics_tracker.py
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
# Problem 3369: Design an Array Statistics Tracker
# https://leetcode.com/problems/design-an-array-statistics-tracker/
from collections import deque
from sortedcontainers import SortedList
class StatisticsTracker:
def __init__(self):
self.nums = deque()
self.sorted_nums = SortedList([])
self.sum_nums = 0
self.n = 0
self.counter = {}
def add_number(self, number: int) -> None:
self.nums.append(number)
self.sorted_nums.add(number)
self.sum_nums += number
self.n += 1
if number not in self.counter:
self.counter[number] = 0
self.counter[number] += 1
def remove_first_added_number(self) -> None:
if self.nums:
first_num = self.nums[0]
self.nums.remove(first_num)
self.sorted_nums.remove(first_num)
self.sum_nums -= first_num
self.n -= 1
self.counter[first_num] -= 1
if self.counter[first_num] == 0:
del self.counter[first_num]
def get_mean(self) -> int:
return self.sum_nums // self.n if self.n > 0 else 0
def get_median(self) -> int:
if self.n == 0:
return 0
return self.sorted_nums[self.n//2]
def get_mode(self) -> int:
if not self.counter:
return 0
max_count = max(self.counter.values())
modes = [key for key, value in self.counter.items() if value == max_count]
return min(modes)
# Your StatisticsTracker object will be instantiated and called as such:
# obj = StatisticsTracker()
# obj.add_number(number)
# obj.remove_first_added_number()
# param_3 = obj.get_mean()
# param_4 = obj.get_median()
# param_5 = obj.get_mode()