Implement a simplified version of Twitter which allows users to post tweets, follow/unfollow each other, and view the 10 most recent tweets within their own news feed.
Implement the Twitter class:
Twitter() Initializes your twitter object.postTweet(userId, tweetId) Publish a new tweet by the user userId.getNewsFeed(userId) Fetches at most the 10 most recent tweet IDs in the user's feed. Each item must be posted by users who the user followed or by the user themself. ordered from most recent to least recent.follow(followerId, followeeId) The user with followerId started following the user with followeeId.unfollow(followerId, followeeId) The user with followerId started unfollowing the user with followeeId.1 <= userId, followerId, followeeId <= 5000 <= tweetId <= 10^4tweetIds are unique.3 * 10^4 calls will be made to postTweet, getNewsFeed, follow, and unfollow.Why do it this way? To generate a user's news feed, the most intuitive approach is to just grab all of the user's own tweets, plus all the tweets of everyone they follow. We dump them all into one giant list, sort the list by timestamp to get the most recent ones first, and return the top 10. It's incredibly simple, but pulling every single tweet just to get the top 10 is very inefficient for active users.
Imagine reading the news by ordering every single newspaper published in the entire world over the last decade, sorting them all by date in your living room, and then only reading the front page of the 10 newest ones.
class Twitter:
def getNewsFeed(self, userId):
all_tweets = []
# Add user's own tweets
all_tweets.extend(self.tweets[userId])
# Add followees' tweets
for followeeId in self.follow_map[userId]:
all_tweets.extend(self.tweets[followeeId])
# Sort all by time (descending)
all_tweets.sort(by=time)
# Return top 10
return [tweet.id for tweet in all_tweets[:10]]Why do it this way? Because tweets for each user are naturally appended chronologically, each user's tweet list is already sorted by time! The problem of getting the 10 most recent tweets across `F` followees is exactly the "Merge K Sorted Lists" problem. We put the single most recent tweet of every followee into a Min-Heap. We pop the most recent one, add it to our feed, and push the next most recent tweet from that same user back into the heap. We only do this 10 times.
You have 5 reporters in the field. Instead of having them send you their entire life's work, you just ask each reporter for their single newest story. You look at the 5 stories, broadcast the newest one, and then ask that specific reporter for their next newest story. You do this 10 times and you're done.
class Twitter:
def getNewsFeed(self, userId):
minHeap = []
res = []
# Add the newest tweet from user and followees to heap
users = self.follow_map[userId] + [userId]
for u in users:
if self.tweets[u]:
newest_tweet = self.tweets[u][-1]
minHeap.push(newest_tweet)
# Pop the 10 newest tweets overall
while minHeap and len(res) < 10:
tweet = minHeap.pop()
res.append(tweet.id)
# Push the next newest tweet from the SAME user
if tweet.has_older_tweet():
minHeap.push(tweet.get_older_tweet())
return res
Discussion
…