示例#1
0
        public IList <IList <int> > GetSkyline(int[][] buildings)
        {
            var sortedList = new SortedList <int, IList <int> >();

            foreach (var building in buildings)
            {
                if (!sortedList.ContainsKey(building[0]))
                {
                    sortedList.Add(building[0], new List <int>());
                }
                sortedList[building[0]].Add(-building[2]);

                if (!sortedList.ContainsKey(building[1]))
                {
                    sortedList.Add(building[1], new List <int>());
                }
                sortedList[building[1]].Add(building[2]);
            }

            var result  = new List <IList <int> >();
            var heights = new MaxPriorityQueue();

            foreach (var x in sortedList.Keys)
            {
                foreach (var height in sortedList[x])
                {
                    if (height < 0)
                    {
                        heights.Insert(-height);
                    }
                    else
                    {
                        heights.Delete(height);
                    }
                }

                if (heights.Size() == 0)
                {
                    result.Add(new List <int>()
                    {
                        x, 0
                    });
                }
                else
                {
                    var height = heights.Max();
                    if (result.Count == 0 || result[result.Count - 1][1] != height)
                    {
                        result.Add(new List <int>()
                        {
                            x, height
                        });
                    }
                }
            }

            return(result);
        }