# 295. Find Median from Data Stream

**<font color=red>难度: Hard</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/find-median-from-data-stream/description/

> 内容描述

```
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.

For example,
[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.
 

Example:

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3) 
findMedian() -> 2
 

Follow up:

If all integer numbers from the stream are between 0 and 100, how would you optimize it?
If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******

二分插入，插入后要挪元素位置，总时间为O(N)

beats 93.41%

```python
class MedianFinder:

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

    def addNum(self, num):
        """
        :type num: int
        :rtype: void
        """
        bisect.insort(self.lst, num)
        

    def findMedian(self):
        """
        :rtype: float
        """
        return (self.lst[len(self.lst) / 2] + self.lst[~(len(self.lst) / 2)]) / 2.0
```



> 思路 2
******- 时间复杂度: O(lgN)******- 空间复杂度: O(N)******



两个堆，一个放更大的那一半数字，一个放更小的那一半数字

- Time complexity: O(3 * log(n)) + O(1) ≈ O(log(n))

  - At worst, there are two heap insertions and one heap deletions from the top. 
  Each of these takes about O(log(n)) time.
  - Finding the mean takes constant O(1) time since the tops of heaps are directly accessible.
- Space complexity: O(n) linear space to hold input in containers.

beats 99.86%

```python
from heapq import *
class MedianFinder:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.min_half = []
        self.max_half = [] # 可能会多一个数字
        

    def addNum(self, num):
        """
        :type num: int
        :rtype: void
        """
        num = float(num)
        if len(self.min_half) == len(self.max_half):
            heappush(self.max_half, -heappushpop(self.min_half, -num))
        else:
            heappush(self.min_half, -heappushpop(self.max_half, num))
        

    def findMedian(self):
        """
        :rtype: float
        """
        if len(self.min_half) == len(self.max_half):
            return (-self.min_half[0] + self.max_half[0] ) / 2
        else:
            return self.max_half[0]
```
