My C# solution based on SortedSet


  • 0
    S
    public class Solution {
        public IList<int[]> GetSkyline(int[,] buildings) 
        {
            var height = new List<int[]>();
            var result = new List<int[]>();
            var dic = new Dictionary<int, int>();
            for(int i=0; i<buildings.GetLength(0); i++)
            {
                height.Add(new int[]{buildings[i,0], -buildings[i,2]});
                height.Add(new int[]{buildings[i,1], buildings[i,2]});
            }
            height.Sort((a,b)=>{
                if(a[0] != b[0])
                {
                    return a[0].CompareTo(b[0]);
                }
                return a[1].CompareTo(b[1]);
            });
            var ss = new SortedSet<int>();
            ss.Add(0);
            var pre = 0;
            foreach(var h in height)
            {
                if(h[1]<0)
                {
                    if(!dic.ContainsKey(-h[1]))
                    {
                        dic.Add(-h[1],0);
                    }
                    ss.Add(-h[1]);
                    dic[-h[1]]++;
                }
                else
                {
                    dic[h[1]]--;
                    if(dic[h[1]] <= 0)
                    {
                         ss.Remove(h[1]);
                    }
                }
                int cur = ss.Max;
                if(pre != cur)
                {
                    result.Add(new int[]{h[0], cur});
                    pre =cur;
                }
            }
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.