Created
June 9, 2020 04:11
-
-
Save RP-3/e8554375d87b5850107f1be7e7479de8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Initialize your data structure here. | |
*/ | |
var Twitter = function() { | |
this.tweets = {}; // 1: [[5,0]] | |
this.following = {}; | |
this.tweetCount = 0; | |
}; | |
/** | |
* Compose a new tweet. | |
* @param {number} userId | |
* @param {number} tweetId | |
* @return {void} | |
*/ | |
Twitter.prototype.postTweet = function(userId, tweetId) { | |
if(!this.tweets.hasOwnProperty(userId)) this.tweets[userId] = []; | |
this.tweets[userId].push([tweetId, this.tweetCount++]); | |
}; | |
/** | |
* 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. | |
* @param {number} userId | |
* @return {number[]} | |
*/ | |
Twitter.prototype.getNewsFeed = function(userId) { | |
if(!this.following.hasOwnProperty(userId)) this.following[userId] = new Set(); | |
if(!this.tweets[userId]) this.tweets[userId] = []; | |
const followedTweets = [...this.tweets[userId]]; | |
for(let followee of this.following[userId]){ | |
followedTweets.push(...(this.tweets[followee] || [])); | |
} | |
const seenTweets = new Set(); | |
return followedTweets | |
.sort((a, b) => b[1] - a[1]) // desc order of recency | |
.filter(([tweetId, _]) => { // remove duplicates | |
if(seenTweets.has(tweetId)) return false; | |
seenTweets.add(tweetId); | |
return true; | |
}) | |
.slice(0, 10) // pick the first 10 | |
.map(([tweetId, _]) => tweetId); // fetch just the tweets | |
}; | |
/** | |
* Follower follows a followee. If the operation is invalid, it should be a no-op. | |
* @param {number} followerId | |
* @param {number} followeeId | |
* @return {void} | |
*/ | |
Twitter.prototype.follow = function(followerId, followeeId) { | |
if(!this.following.hasOwnProperty(followerId)) this.following[followerId] = new Set(); | |
this.following[followerId].add(followeeId); | |
}; | |
/** | |
* Follower unfollows a followee. If the operation is invalid, it should be a no-op. | |
* @param {number} followerId | |
* @param {number} followeeId | |
* @return {void} | |
*/ | |
Twitter.prototype.unfollow = function(followerId, followeeId) { | |
if(!this.following.hasOwnProperty(followerId)) this.following[followerId] = new Set(); | |
this.following[followerId].delete(followeeId); | |
}; | |
/** | |
* Your Twitter object will be instantiated and called as such: | |
* var obj = new Twitter() | |
* obj.postTweet(userId,tweetId) | |
* var param_2 = obj.getNewsFeed(userId) | |
* obj.follow(followerId,followeeId) | |
* obj.unfollow(followerId,followeeId) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment