Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions find-median-from-data-stream/tedkimdev.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Heap / Priority Queue, Two Pointers
  • 설명: 이 코드는 두 개의 힙을 사용하여 데이터 스트림의 중앙값을 효율적으로 찾는 방법으로, 힙 구조를 활용하는 패턴과 두 포인터 개념이 결합된 구조입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
type MedianFinder struct {
small *MaxHeap // lower half
large *MinHeap // upper half
}

func Constructor() MedianFinder {
small := &MaxHeap{}
large := &MinHeap{}
heap.Init(small)
heap.Init(large)

return MedianFinder{
small: small,
large: large,
}
}

// SC: O(n)
// TC: O(m * log n)
func (this *MedianFinder) AddNum(num int) {
if this.small.Len() == 0 || num <= (*this.small)[0] {
heap.Push(this.small, num)
} else {
heap.Push(this.large, num)
}

// rebalance
if this.small.Len() > this.large.Len()+1 {
v := heap.Pop(this.small).(int)
heap.Push(this.large, v)
}

if this.large.Len() > this.small.Len() {
v := heap.Pop(this.large).(int)
heap.Push(this.small, v)
}
}

// TC: O(m)
func (this *MedianFinder) FindMedian() float64 {
if this.small.Len() > this.large.Len() {
return float64((*this.small)[0])
}

return float64((*this.small)[0]+(*this.large)[0]) / 2.0
}

type MinHeap []int

func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }

func (h *MinHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}

func (h *MinHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}

type MaxHeap []int

func (h MaxHeap) Len() int { return len(h) }
func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] } // reverse
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }

func (h *MaxHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}

func (h *MaxHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}
Loading