-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cs
60 lines (53 loc) · 1.55 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
public class Comparer : IComparer<Tuple<double, int>>
{
public int Compare(Tuple<double, int> x, Tuple<double, int> y)
{
var result = x.Item1.CompareTo(y.Item1);
if (result != 0)
{
return result;
}
return x.Item2.CompareTo(y.Item2);
}
}
public class MedianFinder {
private SortedSet<Tuple<double, int>> smallerHeap = new SortedSet<Tuple<double, int>>();
private SortedSet<Tuple<double, int>> biggerHeap = new SortedSet<Tuple<double, int>>();
private int index;
public void AddNum(double num) {
if (smallerHeap.Count == 0 || smallerHeap.Max.Item1 >= num)
{
smallerHeap.Add(Tuple.Create(num, index++));
}
else
{
biggerHeap.Add(Tuple.Create(num, index++));
}
if (smallerHeap.Count == biggerHeap.Count + 2)
{
biggerHeap.Add(smallerHeap.Max);
smallerHeap.Remove(smallerHeap.Max);
}
else if (biggerHeap.Count == smallerHeap.Count + 2)
{
smallerHeap.Add(biggerHeap.Min);
biggerHeap.Remove(biggerHeap.Min);
}
}
public double FindMedian() {
if (smallerHeap.Count == biggerHeap.Count)
{
return (smallerHeap.Max.Item1 + biggerHeap.Min.Item1) / 2;
}
else if (smallerHeap.Count < biggerHeap.Count)
{
return biggerHeap.Min.Item1;
}
else
{
return smallerHeap.Max.Item1;
}
}
}