首页 > 其他分享 >LeetCode:295. Find Median from Data Stream

LeetCode:295. Find Median from Data Stream

时间:2022-12-05 18:06:44浏览次数:42  
标签:return Stream int Median heapComm func rightHeap leftHeap Find


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();
*/


标签:return,Stream,int,Median,heapComm,func,rightHeap,leftHeap,Find
From: https://blog.51cto.com/u_15903085/5913197

相关文章