LeetCode 295. Find Median from Data Stream

简介: 中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

v2-505d20440a547280c251a80fc3656da5_1440w.jpg

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.


描述



中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。


例如,

[2,3,4] 的中位数是 3

[2,3] 的中位数是 (2 + 3) / 2 = 2.5

设计一个支持以下两种操作的数据结构:


void addNum(int num) - 从数据流中添加一个整数到数据结构中。

double findMedian() - 返回目前所有元素的中位数。


示例:


addNum(1)

addNum(2)

findMedian() -> 1.5

addNum(3)

findMedian() -> 2


进阶:

如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法?

如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法?


思路



  • 使用python的bisect库,维护一个有序的数组.
  • 每次我们返回中间值即可


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-02-08 13:58:09
# @Last Modified by:   何睿
# @Last Modified time: 2019-02-08 13:58:09
# 二分搜索库
import bisect
class MedianFinder:
    def __init__(self):
        """
        initialize your data structure here.
        """
        # 声明一个数组
        self.list = []
        # 数组中元素的个数
        self.count = 0
    def addNum(self, num: 'int') -> 'None':
        # 将元素个数自增一次
        self.count += 1
        # 插入新元素
        bisect.insort_left(self.list, num)
    def findMedian(self) -> 'float':
        # 如果是奇数,返回中间值
        if self.count % 2:
            return self.list[self.count // 2]
        else:
            # 如果是偶数,返回中间两个数的平均值
            num1 = self.count // 2
            return (self.list[num1] + self.list[num1 - 1]) / 2

源代码文件在这里.


目录
相关文章
|
7月前
|
Java
Leetcode 295. Find Median from Data Stream
在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
30 0
|
7月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
28 0
|
算法 Python
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
|
JavaScript 索引
LeetCode 436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
63 0
LeetCode 436. Find Right Interval
LeetCode 389. Find the Difference
给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
88 0
LeetCode 389. Find the Difference
|
索引
LeetCode 373. Find K Pairs with Smallest Sums
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
122 0
LeetCode 373. Find K Pairs with Smallest Sums
|
索引
LeetCode 287. Find the Duplicate Number
给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
72 0
LeetCode 287. Find the Duplicate Number
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
11天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
11天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-1
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题