You are viewing a single comment's thread. Return to all comments →
C#
var pre = new PriorityQueue<int, int>(); var post = new PriorityQueue<int, int>(); var medians = new List<double>(); foreach(var value in a) { if (pre.Count == 0 || value < pre.Peek()) { pre.Enqueue(value, -value); } else { post.Enqueue(value, value); } if (pre.Count > post.Count + 1) { var temp = pre.Dequeue(); post.Enqueue(temp, temp); } else if (post.Count > pre.Count) { var temp = post.Dequeue(); pre.Enqueue(temp, -temp); } if (pre.Count != post.Count) { medians.Add(pre.Peek()); } else { medians.Add((post.Peek() + pre.Peek()) / 2.0); } } return medians;
Seems like cookies are disabled on this browser, please enable them to open this website
Find the Running Median
You are viewing a single comment's thread. Return to all comments →
C#