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

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:

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

--------------------------------------------------------------------------

用堆很容易想到,主要是两个堆顶和新加的数有6种情况,应该放在哪个堆又有两种情况,写起来一堆if else

所以先找个堆放进去,然后再决定要不要调整,codes就简洁很多:

import heapq

class MedianFinder:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.mi_heap = []
        self.ma_heap = []
        
    def addNum(self, num: int) -> None:
        heapq.heappush(self.ma_heap,-num)
        top = -heapq.heappop(self.ma_heap)
        heapq.heappush(self.mi_heap, top)
        if (len(self.mi_heap) > len(self.ma_heap)):
            mi_top = heapq.heappop(self.mi_heap)
            heapq.heappush(self.ma_heap,-mi_top)

    def findMedian(self) -> float:
        l1, l2 = len(self.ma_heap),len(self.mi_heap)
        if (l1 == 0):
            return None
        if (l1 == l2):
            return (self.mi_heap[0]-self.ma_heap[0]) / 2
        return -self.ma_heap[0]
        

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

Copy from discussion about extensions:

Followup #1 - If all integer numbers from the stream are between 0 and 100, how would you optimize it

  1. Create 100 buckets using an array of size 100.
  2. Store the numbers into these buckets.
  3. Find median by looping through this array.

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

  1. Divide problem into 3 subproblems. Here are the groupings:
    1. Numbers < 0: You have 2 options:
      1. Use 2-heap solution (that we coded in original solution), or
      2. Use 1 array, which represents 1 bucket
    2. 0 <= Numbers <= 100: Use 100 buckets using an array of size 100
    3. 100 < Numbers: You have 2 options:
      1. Use 2-heap solution (that we coded in original solution), or
      2. Use 1 array, which represents 1 bucket
  2. For each number we get in the stream, insert it into 1 of the 3 groupings, keeping track of the count of numbers in each of these 3 groupings
  3. To find the median, see which grouping the median must fall into and find it there.

 

For Numbers < 0 and 100 < Numbers, using 2 arrays/buckets is the more practical solution since it is very unlikely the median will fall into either bucket/array. This makes findMedian() O(1) in average case. In the worst case, all numbers fall in 1 array, and we would either have to use Quickselect (O(n) average case, O(n2) worst case), or sorting (O(n log n)) to find the median.

 

If you use 2 heaps instead, you will get findMedian() of O(1) average case, O(log n) worst case.

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 《一芯量产工具 FirstChip MpTools 20211024 详解及应用》 在数字存储领域,U 盘作为便携式存储设备的典型代表,其稳定性和可靠性极为关键。而 “FirstChip_MpTools_20211024” 正是专为一芯(FirstChip)芯片打造的 U 盘量产工具,主要用于 FC1178 和 FC1179 系列芯片的 U 盘生产、测试与修复工作。本文将深入剖析该工具的功能、使用方法以及其在 U 盘量产过程中的重要作用。 一芯(FirstChip)是一家专注于存储控制器研发的企业,其 FC1178 和 FC1179 芯片被广泛应用于众多 U 盘产品中。这些芯片具备高性能、低功耗以及高兼容性等诸多优点,但在少数情况下,也可能会出现诸如数据丢失、无法识别等故障,此时就需要借助专业的量产工具来进行修复。 “FirstChip MpTools” 是一款功能强大的一芯 U 盘量产管理软件,具备以下功能:一是进行初始化与格式化操作,能够清除 U 盘中的所有数据,并且可以设置不同的文件系统格式,比如 FAT32、NTFS 或 exFAT,以满足不同用户的多样化需求;二是开展性能测试,通过读写速度测试来评估 U 盘的实际性能,帮助用户判断 U 盘的读写速度是否达到了预期的标准;三是进行坏块检测与修复,扫描 U 盘中可能存在的坏块,并尝试对其进行修复,从而确保 U 盘能够稳定运行;四是实现容量调整,允许用户根据实际需求对 U 盘的可用容量进行调整,在处理扩容盘或者修复容量异常的 U 盘时极为实用;五是进行安全擦除,能够彻底删除 U 盘上的所有数据,确保信息安全无虞;六是开展固件升级,对 U 盘的固件进行更新,以此提高兼容性、修复已知问题或者解锁新的功能。 使用 “FirstChip M
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值