-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution295.cs
35 lines (31 loc) · 891 Bytes
/
Solution295.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
namespace LeetCode.Solutions;
public class Solution295
{
/// <summary>
/// 295. Find Median from Data Stream - Hard
/// <a href="https://leetcode.com/problems/find-median-from-data-stream">See the problem</a>
/// </summary>
public class MedianFinder
{
private readonly List<int> _list;
public MedianFinder() => _list = new List<int>();
public void AddNum(int num)
{
var index = _list.BinarySearch(num);
if (index < 0)
{
index = ~index;
}
_list.Insert(index, num);
}
public double FindMedian()
{
var count = _list.Count;
if (count % 2 == 0)
{
return (_list[count / 2 - 1] + _list[count / 2]) / 2.0;
}
return _list[count / 2];
}
}
}