Skip to content

Latest commit

 

History

History

295-FindMedianfromDataStream

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Find Median from Data Stream

Problem can be found in here!

Solution: Heap

class MedianFinder:
    def __init__(self):
        self.min_heap, self.max_heap = [], []

    def addNum(self, num: int) -> None:
        if len(self.min_heap) < len(self.max_heap):
            heappush(self.min_heap, -heappushpop(self.max_heap, -num))
        else:
            heappush(self.max_heap, -heappushpop(self.min_heap, num))

    def findMedian(self) -> float:
        if len(self.min_heap) == len(self.max_heap):
            return (self.min_heap[0] - self.max_heap[0]) / 2
        else:
            return -self.max_heap[0]

Time Complexity: O(lgn) for adding new number and O(1) for finding the median number, Space Complexity: O(n), where n is the number of numbers.