solution

package
v0.0.0-...-6095842 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2019 License: MIT Imports: 1 Imported by: 0

README

Find Median from Data Stream

Hard

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?

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MedianFinder

type MedianFinder struct {
	// contains filtered or unexported fields
}

func Constructor

func Constructor() MedianFinder

* initialize your data structure here.

func (*MedianFinder) AddNum

func (this *MedianFinder) AddNum(num int)

func (*MedianFinder) FindMedian

func (this *MedianFinder) FindMedian() float64

type MinHeap

type MinHeap struct {
	// contains filtered or unexported fields
}

MinHeap satisfies heap.Interface.

func (MinHeap) Len

func (h MinHeap) Len() int

func (MinHeap) Less

func (h MinHeap) Less(i, j int) bool

func (MinHeap) Min

func (h MinHeap) Min() int

func (*MinHeap) Pop

func (h *MinHeap) Pop() interface{}

func (*MinHeap) Push

func (h *MinHeap) Push(x interface{})

func (MinHeap) Swap

func (h MinHeap) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL