Created
November 12, 2022 16:53
-
-
Save EfrainReyes/0b08cd799b0fda024b61bcddc15cbb72 to your computer and use it in GitHub Desktop.
295. Find Median from Data Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MedianFinder: | |
def __init__(self): | |
self.data = [] | |
def addNum(self, num: int) -> None: | |
bisect.insort(self.data, num) | |
def findMedian(self) -> float: | |
data_length = len(self.data) | |
mid_point = data_length // 2 | |
if data_length % 2 == 0: | |
return (self.data[mid_point] + self.data[mid_point - 1] ) / 2 | |
else: | |
return self.data[mid_point] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment