Skip to content

Latest commit

 

History

History
120 lines (100 loc) · 2.99 KB

295.md

File metadata and controls

120 lines (100 loc) · 2.99 KB
  1. Find Median from Data Stream
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

Examples: 
[2,3,4] , the median is 3

[2,3], the median is (2 + 3) / 2 = 2.5

Design a data structure that supports the following two operations:

void addNum(int num) - Add a integer number from the data stream to the data structure.
double findMedian() - Return the median of all elements so far.
For example:

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3) 
findMedian() -> 2
Credits:
Special thanks to @Louis1992 for adding this problem and creating all test cases.

my thoughts:

1. use two heaps to store the first and second half of the input.
Time: add() -> O(lgn); findMedian() -> O(1)

my solution:

********** 
class MedianFinder:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.h1 = []
        self.h2 = []

    def addNum(self, num):
        """
        :type num: int
        :rtype: void
        """
        if not self.h1:
            heapq.heappush(self.h1, -num)
            return
        if not self.h2:
            if num < -self.h1[0]:
                self.h2.append(-self.h1.pop())
                self.h1.append(-num)
            else:
                self.h2.append(num)
            return
        if num <= self.h2[0] and num >= -self.h1[0]:
            if len(self.h1) > len(self.h2):
                heapq.heappush(self.h2, num)
            else:
                heapq.heappush(self.h1, -num)
        elif num > self.h2[0]:
            if len(self.h1) > len(self.h2):
                heapq.heappush(self.h2, num)
            else:
                temp = heapq.heapreplace(self.h2, num)
                heapq.heappush(self.h1, -temp)
        else:
            if len(self.h1) > len(self.h2):
                temp = heapq.heapreplace(self.h1, -num)
                heapq.heappush(self.h2, -temp)
            else:
                heapq.heappush(self.h1, -num)
                
        #print(self.h1, self.h2)
        
    def findMedian(self):
        """
        :rtype: float
        """
        if not self.h1:
            return 0.0
        if len(self.h1) == len(self.h2):
            return (-self.h1[0] + self.h2[0]) / 2
        else:
            return float(-self.h1[0])


# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()

my comments:


from other ppl's solution:

1. cleaner code:
from heapq import *

class MedianFinder:

    def __init__(self):
        self.heaps = [], []

    def addNum(self, num):
        small, large = self.heaps
        heappush(small, -heappushpop(large, num))
        if len(large) < len(small):
            heappush(large, -heappop(small))

    def findMedian(self):
        small, large = self.heaps
        if len(large) > len(small):
            return float(large[0])
        return (large[0] - small[0]) / 2.0