Exemplo n.º 1
0
        /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by userMap who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
        public IList <int> GetNewsFeed(int userId)
        {
            List <int> res = new List <int>();

            if (!userMap.ContainsKey(userId))
            {
                return(res);
            }

            var             user     = userMap[userId];
            HashSet <int>   followed = user.Followed;
            MaxHeap <Tweet> tweets   = new MaxHeap <Tweet>();

            foreach (var followedUserId in followed)
            {
                var tweetHead = userMap[followedUserId].TweetHead;
                while (tweetHead != null)
                {
                    tweets.Push(tweetHead);
                    tweetHead = tweetHead.Next;
                }
            }

            while (tweets.Any() && res.Count < Limit)
            {
                var tweet = tweets.Pop();
                res.Add(tweet.Id);
            }
            return(res);
        }
Exemplo n.º 2
0
        private static void BalanceHeaps(MinHeap <int> minHeap, MaxHeap <int> maxHeap, int nextNum)
        {
            // An integer from the file is first added to the min-heap.
            // Then while the max-heap peek element is smaller than the min-heap peek element, insert max-heap elements into min-heap.
            minHeap.Push(nextNum);
            while (maxHeap.Any() && maxHeap.Peek() < minHeap.Peek())
            {
                minHeap.Push(maxHeap.Pop());
            }

            // Now we adjust the heap by repeatedly moving min-heap peek element into the max-heap until their size difference is at the max 1 element.
            // Now adjust their sizes to |min size - max size| == 1
            while (System.Math.Abs(minHeap.Count() - maxHeap.Count()) > 1)
            {
                maxHeap.Push(minHeap.Pop());
            }
        }
Exemplo n.º 3
0
            public string FrequencySortWithHeap(string s)
            {
                var map = new Dictionary <char, int>();

                foreach (var c in s)
                {
                    if (!map.ContainsKey(c))
                    {
                        map.Add(c, 0);
                    }
                    map[c]++;
                }

                var heap = new MaxHeap <CharNode>();

                foreach (var i in map.Keys)
                {
                    heap.Push(new CharNode
                    {
                        Value     = i,
                        Frequency = map[i]
                    });
                }

                var sb = new StringBuilder();

                while (heap.Any())
                {
                    var node = heap.Pop();
                    for (var i = 0; i < node.Frequency; i++)
                    {
                        sb.Append(node.Value);
                    }
                }

                return(sb.ToString());
            }