Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and retrieve the 10 most recent tweet IDs in the user's news feed.
Implement the Twitter class:
Twitter()Initializes the twitter object.postTweet(userId: int, tweetId: int) -> NoneComposes a new tweet with IDtweetIdby the useruserId. Each call to this function will have a uniquetweetId.getNewsFeed(userId: int) -> list[int]Retrieves 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 themself. Tweets must be ordered from most recent to least recent.follow(followerId: int, followeeId: int) -> NoneThe user with IDfollowerIdstarted following the user with IDfolloweeId.unfollow(followerId: int, followeeId: int) -> NoneThe user with IDfollowerIdstarted unfollowing the user with IDfolloweeId.
Examples
Example 1
Input:
["Twitter", "postTweet", "getNewsFeed", "follow", "getNewsFeed", "unfollow", "getNewsFeed"] [[], [1, 5], [1], [1, 2], [1], [1, 2], [1]] Output: [null, null, [5], null, [6, 5], null, [5]] twitter = Twitter() twitter.postTweet(1, 5) # User 1 posts tweet 5 twitter.getNewsFeed(1) # return [5], user 1's own tweet twitter.follow(1, 2) # User 1 follows user 2 twitter.postTweet(2, 6) # User 2 posts tweet 6 twitter.getNewsFeed(1) # return [6, 5], tweet 6 is more recent twitter.unfollow(1, 2) # User 1 unfollows user 2 twitter.getNewsFeed(1) # return [5], only user 1's own tweet
Constraints
1 <= userId, followerId, followeeId <= 5000 <= tweetId <= 10⁴- All the tweets have unique IDs.
- At most
3 * 10⁴calls will be made topostTweet,getNewsFeed,follow, andunfollow.
How the tests work: each test is a sequence of calls.
operations[i]is the method called witharguments[i]; the first entry constructs the class. The expected output lists what every call returns —nullfor the constructor and for methods that return nothing.