LeetCode:295. Find Median from Data Stream

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

解题思路 —— 堆

用最大堆来维护左半区间的数据,用最小堆维护右半区间的数据。

  1. 插入数据:
    (1)堆中的元素数为偶数,插入最大堆
    (2)堆中的元素数为奇数,插入最小堆
  2. 计算中位数:
    (1)堆中的元素数为偶数,中位数为最大堆堆顶元素
    (2)堆中的元素数为奇数,中位数为最大堆堆顶元素和最小堆堆顶元素的平均数

AC 代码

/* sort.Interface
type Interface interface {
    // Len is the number of elements in the collection.
    Len() int
    // Less reports whether the element with
    // index i should sort before the element with index j.
    Less(i, j int) bool
    // Swap swaps the elements with indexes i and j.
    Swap(i, j int)
}
*/
/* heap.Interface
type Interface interface {
    sort.Interface
    Push(x interface{}) // add x as element Len()
    Pop() interface{}   // remove and return element Len() - 1.
}
*/
type heapComm []int
func (h heapComm) Len() int           { return len(h) }
func (h heapComm) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
func (h *heapComm) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *heapComm) Pop() interface{} {
    x := (*h)[len(*h)-1]
    *h = (*h)[0 : len(*h)-1]
    return x
}

type minHeap struct {
    heapComm
}
func (h minHeap) Less(i, j int) bool { return h.heapComm[i] < h.heapComm[j] }

type maxHeap struct{
    heapComm
}
func (h maxHeap) Less(i, j int) bool { return h.heapComm[i] > h.heapComm[j] }


type MedianFinder struct {
    leftHeap  maxHeap
    rightHeap minHeap
    length int
}


/** initialize your data structure here. */
func Constructor() MedianFinder {
    var finder MedianFinder
    return finder
}

func (this *MedianFinder) AddNum(num int)  {
    if this.length % 2 == 0 {
        heap.Push(&(this.leftHeap), num)
    } else {
        heap.Push(&(this.rightHeap), num)
    }
    
    // 修复插入元素后最大堆和最小堆元素的关系(最大堆堆顶元素 <= 最小堆堆顶元素)
    if this.length > 1 && this.leftHeap.heapComm[0] > this.rightHeap.heapComm[0]{
        this.leftHeap.heapComm[0], this.rightHeap.heapComm[0] = this.rightHeap.heapComm[0], this.leftHeap.heapComm[0]
        heap.Fix(&this.leftHeap, 0)
        heap.Fix(&this.rightHeap, 0)
    }
    
    this.length++
}


func (this *MedianFinder) FindMedian() float64 {
    if this.length == 0 {
        return 0
    } else if this.length % 2 == 0 {
        return (float64(this.leftHeap.heapComm[0]) + float64(this.rightHeap.heapComm[0])) / 2
    } else {
        return float64(this.leftHeap.heapComm[0])
    }
}


/**
 * Your MedianFinder object will be instantiated and called as such:
 * obj := Constructor();
 * obj.AddNum(num);
 * param_2 := obj.FindMedian();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值