private IEnumerable<Point> GetSkyLine(IEnumerable<Building> buildings) { var buildingsArray = buildings.ToArray(); var leftPoints = new Queue<Point>(buildingsArray.Select(item => new Point(item.Left, item.Height))); var rightPoints = new Queue<Point>(buildingsArray.Select(item => new Point(item.Right, item.Height)).OrderBy(item => item.X)); var heights = new MaxHeap<int>(int.MinValue); heights.Insert(0); int height = 0; while (leftPoints.Count != 0 || rightPoints.Count != 0) { bool takeLeftPoint = leftPoints.Count != 0 && leftPoints.Peek().X < rightPoints.Peek().X; int x; if (takeLeftPoint) { Point point = leftPoints.Dequeue(); x = point.X; heights.Insert(point.Y); } else { Point point = rightPoints.Dequeue(); x = point.X; heights.Remove(point.Y); } int maxHeight = heights.Maximum(); if (maxHeight != height) { height = maxHeight; yield return new Point(x, height); } } }