Python SortedList
Complexity\n- Time complexity: O(NlogN)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(N)\n Add your space complexity here, e.g. O(n) \n\n
# Complexity\n- Time complexity: O(NlogN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfrom sortedcontainers import SortedList\nclass MKAverage:\n\n    def __init__(self, m: int, k: int):\n        self.queue = deque([])\n        self.sorted_list = SortedList()\n        self.m = m\n        self.k = k\n        \n    def addElement(self, num: int) -> None:\n        if len(self.queue) >= self.m: \n            val = self.queue.popleft()\n            self.sorted_list.remove(val)\n        self.queue.append(num)\n        self.sorted_list.add(num)\n        \n    def calculateMKAverage(self) -> int:\n        if len(self.queue) < self.m: return -1\n        # print(self.sorted_list)\n        return sum(\n            self.sorted_list[self.k: len(self.sorted_list) - self.k]\n        ) // (self.m - self.k * 2)\n        \n\n\n# Your MKAverage object will be instantiated and called as such:\n# obj = MKAverage(m, k)\n# obj.addElement(num)\n# param_2 = obj.calculateMKAverage()\n```
['Python3']