LeetCode | 355. Design Twitter | HashMap | Vector

preview_player
Показать описание
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 ID tweetId by the user userId. Each call to this function will be made with a unique tweetId.
getNewsFeed(int userId) 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.
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.

#leetcode #dsa #hashmap #vector
Рекомендации по теме
Комментарии
Автор

Yes It will give TLE and thats because we are iterating through all posts many of which may not be relevant for the given userId getNewsFeed Scans All Posts Every Time:
The method iterates through all posts (from newest to oldest) to find tweets from the user and their followees.
If there are P posts, this takes O(P) time per call. suppose P calse then it becomse O(p^2) time complexity hence TLE

AshishKumar-ypyo
Автор

Great intuition of using 2D vector to save time complexity instead of priority_queue, every other solution was using priority queue. One small correction, both unordered_map and unordered_set have average O(1) time complexity for insert, erase, find etc. It is O(N) in the worst case for both ; which is very rare. So it doesn't matter if we use unordered_map<int, unordered_map> or unordered_map<int, unordered_set>.

dhruvchopra
Автор

you should make more videos...btw clear and concise explanation.

RajanKumar-vfop
join shbcf.ru