Skip to content

Commit 62c70b6

Browse files
committed
✨ Add solution and testcases to problem 295
1 parent 3ff1e32 commit 62c70b6

File tree

3 files changed

+100
-25
lines changed

3 files changed

+100
-25
lines changed

leetcode/201-300/0295.Find-Median-from-Data-Stream/README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
# [295.Find Median from Data Stream][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

5+
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.
6+
7+
For example, for arr = [2,3,4], the median is 3.
8+
For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
9+
10+
Implement the MedianFinder class:
11+
12+
MedianFinder() initializes the MedianFinder object.
13+
void addNum(int num) adds the integer num from the data stream to the data structure.
14+
double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.
15+
16+
817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input
21+
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
22+
[[], [1], [2], [], [3], []]
23+
Output
24+
[null, null, null, 1.5, null, 2.0]
1325
```
1426

1527
## 题意
Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "container/heap"
4+
5+
type IntHeap []int
6+
7+
func (ih IntHeap) Len() int { return len(ih) }
8+
func (ih IntHeap) Less(i, j int) bool { return ih[i] < ih[j] }
9+
func (ih IntHeap) Swap(i, j int) { ih[i], ih[j] = ih[j], ih[i] }
10+
func (ih *IntHeap) Push(num interface{}) {
11+
*ih = append(*ih, num.(int))
12+
}
13+
func (ih *IntHeap) Pop() interface{} {
14+
old := *ih
15+
n := ih.Len()
16+
num := old[n - 1]
17+
*ih = old[: n - 1]
18+
return num
19+
}
20+
21+
type MedianFinder struct {
22+
Small IntHeap
23+
Large IntHeap
524
}
25+
26+
func Constructor() MedianFinder {
27+
return MedianFinder{[]int{}, []int{}}
28+
}
29+
30+
func (mf *MedianFinder) AddNum(num int) {
31+
if mf.Small.Len() == mf.Large.Len() {
32+
heap.Push(&mf.Large, -num)
33+
val := heap.Pop(&mf.Large)
34+
heap.Push(&mf.Small, -(val.(int)))
35+
} else {
36+
heap.Push(&mf.Small, num)
37+
val := heap.Pop(&mf.Small)
38+
heap.Push(&mf.Large, -(val.(int)))
39+
}
40+
}
41+
42+
func (mf *MedianFinder) FindMedian() float64 {
43+
if mf.Small.Len() == mf.Large.Len() {
44+
return float64(mf.Small[0] - mf.Large[0]) / 2
45+
}
46+
return float64(mf.Small[0])
47+
}

leetcode/201-300/0295.Find-Median-from-Data-Stream/Solution_test.go

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,52 @@ package Solution
22

33
import (
44
"reflect"
5-
"strconv"
65
"testing"
76
)
87

98
func TestSolution(t *testing.T) {
109
// 测试用例
11-
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
15-
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
10+
var medianFinder MedianFinder
11+
var median float64
12+
13+
// Test Case 1
14+
medianFinder = Constructor()
15+
medianFinder.AddNum(1)
16+
medianFinder.AddNum(2)
17+
median = medianFinder.FindMedian()
18+
if !reflect.DeepEqual(median, 1.5) {
19+
t.Fatalf("expected: %v, but got: %v", 1.5, median)
20+
}
21+
medianFinder.AddNum(3)
22+
median = medianFinder.FindMedian()
23+
if !reflect.DeepEqual(median, 2.0) {
24+
t.Fatalf("expected: %v, but got: %v", 2.0, median)
1925
}
2026

21-
// 开始测试
22-
for i, c := range cases {
23-
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25-
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
28-
}
29-
})
27+
// Test Case 2
28+
medianFinder = Constructor()
29+
medianFinder.AddNum(2)
30+
medianFinder.AddNum(1)
31+
median = medianFinder.FindMedian()
32+
if !reflect.DeepEqual(median, 1.5) {
33+
t.Fatalf("expected: %v, but got: %v", 1.5, median)
34+
}
35+
medianFinder.AddNum(5)
36+
median = medianFinder.FindMedian()
37+
if !reflect.DeepEqual(median, 2.0) {
38+
t.Fatalf("expected: %v, but got: %v", 2.0, median)
39+
}
40+
medianFinder.AddNum(7)
41+
medianFinder.AddNum(2)
42+
median = medianFinder.FindMedian()
43+
if !reflect.DeepEqual(median, 2.0) {
44+
t.Fatalf("expected: %v, but got: %v", 2.0, median)
45+
}
46+
medianFinder.AddNum(0)
47+
medianFinder.AddNum(5)
48+
median = medianFinder.FindMedian()
49+
if !reflect.DeepEqual(median, 2.0) {
50+
t.Fatalf("expected: %v, but got: %v", 2.0, median)
3051
}
3152
}
3253

0 commit comments

Comments
 (0)