Medium difficulty · Question · Topics: design, hash-table, heap
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and see the 10 most recent tweets in the user's news feed.
Implement the Twitter class:
Twitter() initializes your twitter object.void postTweet(int userId, int tweetId) composes a new tweet with ID tweetId by the user userId. Each call to this function is made with a unique tweetId.List<Integer> getNewsFeed(int userId) retrieves the 10 most recent tweet IDs in the user's news feed. Each item must be posted by users who the user followed or by the user themself. Tweets must be ordered from most recent to least recent.void follow(int followerId, int followeeId) the user with ID followerId started following the user with ID followeeId.void unfollow(int followerId, int followeeId) the user with ID followerId started unfollowing the user with ID followeeId.Input
["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"]
[[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]]
Output
[null, null, [5], null, null, [6, 5], null, [5]]
1 <= userId, followerId, followeeId <= 5000 <= tweetId <= 10^43 * 10^4 calls will be made to postTweet, getNewsFeed, follow, and unfollow.class Solution {
constructor() {
this.count=0;
this.users = {};
this.tweetMap={};
}
/**
* @param {number} userId
* @param {number} tweetId
* @return {void}
*/
postTweet(userId, tweetId) {
const tweetList = this.tweetMap[userId] ? [...this.tweetMap[userId], [this.count,tweetId]] : [[this.count, tweetId]];
if(tweetList.length > 10) tweetList.shift();
this.tweetMap[userId] = tweetList;
this.count--;
}
/**
* @param {number} userId
* @return {number[]}
*/
getNewsFeed(userId) {
const res = [];
if(!this.users[userId]) this.users[userId] = new Set();
this.users[userId] = this.users[userId].add(userId);
const minHeap = new PriorityQueue((a,b) => a[0]-b[0]);
if(this.users[userId].size >= 10){
const maxHeap = new PriorityQueue((a, b) => b[0]-a[0]);
for(const id of this.users[userId]){
if(!this.tweetMap[id]) continue;
const tweets = this.tweetMap[id];
const idx = tweets.length - 1;
const [cnt, tId] = tweets[idx];
maxHeap.enqueue([cnt, tId, id, idx - 1]);
if(maxHeap.size() > 10) maxHeap.dequeue();
}
while(maxHeap.size()){
minHeap.enqueue(maxHeap.dequeue());
}
}else{
for(const id of this.users[userId]){
if(!this.tweetMap[id]) continue;
const tweets = this.tweetMap[id];
const idx = tweets.length - 1;
const [cnt, tId] = tweets[idx];
minHeap.enqueue([cnt, tId, id, idx - 1]);
}
}
while(minHeap.size() > 0 && res.length < 10){
const [_, tId, fId, idx] = minHeap.dequeue();
res.push(tId);
if (idx >= 0) {
const [olderCount, olderTId] = this.tweetMap[fId][idx];
minHeap.enqueue([olderCount, olderTId, fId, idx - 1]);
}
}
return res;
}
/**
* @param {number} followerId
* @param {number} followeeId
* @return {void}
*/
follow(followerId, followeeId) {
const setOfFollowers = this.users[followerId] ? this.users[followerId] : new Set();
setOfFollowers.add(followeeId)
this.users[followerId] = setOfFollowers;
}
/**
* @param {number} followerId
* @param {number} followeeId
* @return {void}
*/
unfollow(followerId, followeeId) {
const setOfFollowers = this.users[followerId] ? this.users[followerId] : null;
if(!setOfFollowers) return;
setOfFollowers.delete(followeeId);
this.users[followerId] = setOfFollowers;
}
}