355. Design Twitter
Description
Difficulty: Medium
Related Topics: Hash Table, Linked List, Design, Heap (Priority Queue)
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to 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 IDtweetId
by the useruserId
. Each call to this function will be made with a uniquetweetId
.List<Integer> getNewsFeed(int userId)
Retrieves the10
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.void follow(int followerId, int followeeId)
The user with IDfollowerId
started following the user with IDfolloweeId
.void unfollow(int followerId, int followeeId)
The user with IDfollowerId
started unfollowing the user with IDfolloweeId
.
Example 1:
1 | Input |
Constraints:
1 <= userId, followerId, followeeId <= 500
- 0 <= tweetId <= 104
- All the tweets have unique IDs.
- At most 3 * 104 calls will be made to
postTweet
,getNewsFeed
,follow
, andunfollow
.
Hints/Notes
- 2023/10/24
- priority queue + multiple maps
- the timestamp needs to be recorded separately
- No solution from 0x3F
Solution
Language: C++
1 | class Twitter { |