Beispiel #1
0
        public void AddNum(int num)
        {
            _minIntHeap.Push(num);
            _maxIntHeap.Push(_minIntHeap.Pop());

            if (_minIntHeap.GetSize() < _maxIntHeap.GetSize())
            {
                _minIntHeap.Push(_maxIntHeap.Pop());
            }
        }
Beispiel #2
0
        public IList <string> TopKFrequent(string[] words, int k)
        {
            var result = new List <string>();
            var heap   = new MaxIntHeap(words.Length);

            var dictionary = BuildFrequencyDictionary(words);

            foreach (var pair in dictionary)
            {
                heap.Push(pair);
            }


            while (k > 0)
            {
                var nextResult = heap.Pop();
                result.Add(nextResult.Key + " " + nextResult.Value);
                k--;
            }
            return(result);
        }
Beispiel #3
0
        public IList <IList <int> > GetSkyline(int[][] buildings)
        {
            var result = new List <IList <int> >();

            int[] x2Buildings = new int[buildings.GetLength(0)], heights = new int[buildings.GetLength(0)];
            for (int i = 0; i < x2Buildings.Length; i++)
            {
                x2Buildings[i] = buildings[i][1]; heights[i] = buildings[i][2];
            }
            Array.Sort(x2Buildings, heights);                     //Sort x2 and height seqences
            int[,] xAll = new int[buildings.GetLength(0) * 2, 3]; //0: x, 1: height, 2: left/right
            for (int i = 0, j = 0; i < buildings.GetLength(0) || j < x2Buildings.Length;)
            {                                                     // Merging Sort all edges
                int x1 = i < buildings.Length ? buildings[i][0] : int.MaxValue;
                int x2 = j < x2Buildings.Length ? x2Buildings[j] : int.MaxValue;
                if (j == x2Buildings.Length || i < buildings.GetLength(0) && x1 <= x2)
                {
                    xAll[i + j, 0] = buildings[i][0];
                    xAll[i + j, 1] = buildings[i][2];
                    xAll[i + j, 2] = 0;
                    i++;
                }
                else
                {
                    xAll[i + j, 0] = x2Buildings[j];
                    xAll[i + j, 1] = heights[j];
                    xAll[i + j, 2] = 1;
                    j++;
                }
            }

            var curHeights = new Dictionary <int, int>();
            var sH         = new MaxIntHeap();

            for (int i = 0; i < xAll.GetLength(0); i++)
            {
                if (xAll[i, 2] == 0)
                {
                    curHeights[xAll[i, 1]] = curHeights.ContainsKey(xAll[i, 1]) ? curHeights[xAll[i, 1]] + 1 : 1;
                    sH.Push(xAll[i, 1]);
                }
                else
                {
                    if (curHeights[xAll[i, 1]] > 1)
                    {
                        curHeights[xAll[i, 1]]--;
                    }
                    else
                    {
                        curHeights.Remove(xAll[i, 1]);
                        sH.Push(xAll[i, 1]);
                    }
                }
                int curMaxHeight = curHeights.Count == 0 ? 0 : sH.Peek();
                if (result.Count > 0 && result[result.Count - 1][1] == curMaxHeight)
                {
                    continue;
                }
                else if (result.Count > 0 && result[result.Count - 1][0] == xAll[i, 0])
                {
                    result[result.Count - 1][1] = curMaxHeight;
                }
                else
                {
                    result.Add(new int[] { xAll[i, 0], curMaxHeight });
                }
            }
            return(result);
        }