leetcode295 Find Median from Data Stream

 1 """
 2 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.
 3 For example,
 4 [2,3,4], the median is 3
 5 [2,3], the median is (2 + 3) / 2 = 2.5
 6 Design a data structure that supports the following two operations:
 7     void addNum(int num) - Add a integer number from the data stream to the data structure.
 8     double findMedian() - Return the median of all elements so far.
 9 Example:
10 addNum(1)
11 addNum(2)
12 findMedian() -> 1.5
13 addNum(3)
14 findMedian() -> 2
15 """
16 """
17 用一个大顶堆和一个小顶堆来维护数据,
18 每次每个数进来,先把它丢进小顶堆,然后把小顶堆的堆顶丢进大顶堆,
19 调整两个堆,使得size 差最大为1。
20 这么搞的好处在于,小顶堆是数据流里前一半大的数,大顶堆是数据流里后一半的大的数,
21 而且小顶堆的size一定 >= 大顶堆的size,
22 小顶堆的堆顶M是小顶堆里最小的数,大顶堆的堆顶N是大顶堆里最大的数,
23 如果两个堆的size相同,那么中位数就是return (M + N) / 2.0
24 否则,return M / 1.0。
25 注意python没有大顶堆,所以放进大顶堆的数乘了-1, 取出来的时候也要记得 * -1
26 传送门:https://blog.csdn.net/qq_32424059/article/details/90346347
27 """
28
29 from heapq import *
30 class MedianFinder:
31
32     def __init__(self):
33         """
34         initialize your data structure here.
35         """
36         self.max_h = list()
37         self.min_h = list()
38         heapify(self.max_h)
39         heapify(self.min_h)
40
41     def addNum(self, num: int) -> None:
42         heappush(self.min_h, num)
43         heappush(self.max_h, -heappop(self.min_h))
44         if len(self.max_h) > len(self.min_h):
45             heappush(self.min_h, -heappop(self.max_h))
46
47     def findMedian(self) -> float:
48         max_len = len(self.max_h)
49         min_len = len(self.min_h)
50         if max_len == min_len:  # 有两个候选中位数
51             return (self.min_h[0] + -self.max_h[0]) / 2.
52         else:  # 小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶
53             return self.min_h[0] / 1.

原文地址:https://www.cnblogs.com/yawenw/p/12357439.html

时间: 2024-10-08 18:17:20

leetcode295 Find Median from Data Stream的相关文章

[LeetCode] Find Median from Data Stream

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

leetcode笔记: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 De

支线任务8-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 Des

LeetCode之Find Median from Data Stream

1.题目描述 翻译过来就是对输入的数字找出其中位数. 2.解题思路 一开始,我想着这是动态输入,所以必须放弃数组这些静态的数据结构.首先想到了平衡二叉树,然而太麻烦了.后面又想到了大顶堆以及小顶堆,但是不知如何运用,就上这道题discuss瞅了瞅.结果发现了一种double heap解法,十分带感,总体思路就是比中位数大的数字放一边(大数堆),小的放另一边(小数堆).然后也接触了并且运用一种新的队列用法——优先队列.插入数字过程中,始终保持大数堆大小大于等于小数堆,以便最后输出中位数.代码如下:

Find Median from Data Stream

常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector<int>& coll,int idx,int max){ int left=2*idx+1,right=2*idx+2; int largest=idx; if(left<max&&coll[left]>coll[idx]) largest=left; if(rig

数据结构与算法(1)支线任务8——Find Median from Data Stream

题目如下:(https://leetcode.com/problems/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] 

解题报告:295. 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 Desi

295. 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 d

[LeetCode] 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 d