Exemplo n.º 1
0
        public void AddNum(int num)
        {
            _minIntHeap.Push(num);
            _maxIntHeap.Push(_minIntHeap.Pop());

            if (_minIntHeap.GetSize() < _maxIntHeap.GetSize())
            {
                _minIntHeap.Push(_maxIntHeap.Pop());
            }
        }
Exemplo n.º 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);
        }