Problem solution · C++

Design Twitter

Design Twitter: a C++ solution using heap or priority queue. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Heap or priority queue
Source
walkccc LeetCode Solutions
Length
106 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Heap or priority queue

For Design Twitter, the implementation repeatedly takes the currently best candidate from a heap while inserting newly available choices.

  1. Define the priority key and whether the smallest or largest item should lead.
  2. Push each candidate when it becomes eligible.
  3. Discard stale entries when necessary and process the best live candidate.

Code notes

  • 106 lines of C++ from the credited upstream file 355.cpp.
  • The implementation visibly relies on sequence storage, hash lookup, work queue.
  • 2 loop blocks detected.

Complexity

Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeDesign Twitter · C++C++
Use this to learn the idea, then write your own version.
struct Tweet {  int id;  int time;  Tweet* next = nullptr;}; struct User {  int id;  unordered_set<int> followeeIds;  Tweet* tweetHead = nullptr;   User() {}   User(int id) : id(id) {    follow(id);  }   void follow(int followeeId) {    followeeIds.insert(followeeId);  }   void unfollow(int followeeId) {    followeeIds.erase(followeeId);  }   void post(int tweetId, int time) {    Tweet* oldTweetHead = tweetHead;    tweetHead = new Tweet(tweetId, time);    tweetHead->next = oldTweetHead;  }}; class Twitter { public:  /** Compose a new tweet. */  void postTweet(int userId, int tweetId) {    if (!users.contains(userId))      users[userId] = User(userId);    users[userId].post(tweetId, time++);  }   /**   * Retrieve the 10 most recent tweet ids in the user's news feed. Each item in   * the news feed must be posted by users who the user followed or by the user   * herself. Tweets must be ordered from most recent to least recent.   */  vector<int> getNewsFeed(int userId) {    if (!users.contains(userId))      return {};     vector<int> newsFeed;     auto compare = [](const Tweet* a, const Tweet* b) {      return a->time < b->time;    };    priority_queue<Tweet*, vector<Tweet*>, decltype(compare)> maxHeap(compare);     for (const int followeeId : users[userId].followeeIds) {      Tweet* tweetHead = users[followeeId].tweetHead;      if (tweetHead != nullptr)        maxHeap.push(tweetHead);    }     int count = 0;    while (!maxHeap.empty() && count++ < 10) {      Tweet* tweet = maxHeap.top();      maxHeap.pop();      newsFeed.push_back(tweet->id);      if (tweet->next)        maxHeap.push(tweet->next);    }     return newsFeed;  }   /**   * Follower follows a followee.   * If the operation is invalid, it should be a no-op.   */  void follow(int followerId, int followeeId) {    if (followerId == followeeId)      return;    if (!users.contains(followerId))      users[followerId] = User(followerId);    if (!users.contains(followeeId))      users[followeeId] = User(followeeId);    users[followerId].follow(followeeId);  }   /**   * Follower unfollows a followee.   * If the operation is invalid, it should be a no-op.   */  void unfollow(int followerId, int followeeId) {    if (followerId == followeeId)      return;    if (const auto it = users.find(followerId);        it != users.cend() && users.contains(followeeId))      it->second.unfollow(followeeId);  }  private:  int time = 0;  unordered_map<int, User> users;  // {userId: User}}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗