355. Design Twitter
Problem:
A genius approach is to use linked list to store tweets in user object. Get news feed is to merge k sorted list.
A small trick is that when initializing user, let the current user follow himself. So that we dont need to add current user to the user list to get news feed.
Should be very careful with unfollow, since the user follows himself. We need to make sure the user wont unfollow himself. Otherwise we wouldn't get the news feed from the current user.
Solution:
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. Your design should support the following methods:
- postTweet(userId, tweetId): Compose a new tweet.
- getNewsFeed(userId): 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.
- follow(followerId, followeeId): Follower follows a followee.
- unfollow(followerId, followeeId): Follower unfollows a followee.
Example:
Twitter twitter = new Twitter(); // User 1 posts a new tweet (id = 5). twitter.postTweet(1, 5); // User 1's news feed should return a list with 1 tweet id -> [5]. twitter.getNewsFeed(1); // User 1 follows user 2. twitter.follow(1, 2); // User 2 posts a new tweet (id = 6). twitter.postTweet(2, 6); // User 1's news feed should return a list with 2 tweet ids -> [6, 5]. // Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5. twitter.getNewsFeed(1); // User 1 unfollows user 2. twitter.unfollow(1, 2); // User 1's news feed should return a list with 1 tweet id -> [5], // since user 1 is no longer following user 2. twitter.getNewsFeed(1);Analysis:
A genius approach is to use linked list to store tweets in user object. Get news feed is to merge k sorted list.
A small trick is that when initializing user, let the current user follow himself. So that we dont need to add current user to the user list to get news feed.
Should be very careful with unfollow, since the user follows himself. We need to make sure the user wont unfollow himself. Otherwise we wouldn't get the news feed from the current user.
Solution:
class Twitter { Map<Integer, User> users = new HashMap<>(); int timeStamp = 0; class Tweet { public int tweetId; public Tweet next; public int time; public Tweet (int tweetId) { this.tweetId = tweetId; time = timeStamp++; } } class User { public int userId; public Tweet head; public Set<Integer> followed; public User(int userId) { this.userId = userId; followed = new HashSet<>(); // follow himself so that can get all tweets' users at once. followed.add(userId); } public void follow(int followeeId) { followed.add(followeeId); } public void unfollow(int followeeId) { followed.remove(followeeId); } public void post(int tweetId) { Tweet tweet = new Tweet(tweetId); if (head == null) { head = tweet; } else { tweet.next = head; head = tweet; } } } /** Initialize your data structure here. */ public Twitter() { } /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { if (!users.containsKey(userId)) { User user = new User(userId); users.put(userId, user); } users.get(userId).post(tweetId); } /** 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. */ public List<Integer> getNewsFeed(int userId) { PriorityQueue<Tweet> queue = new PriorityQueue<>((a, b) -> b.time - a.time); List<Integer> res = new ArrayList<>(); if (!users.containsKey(userId)) { return res; } for(Integer id: users.get(userId).followed) { if (users.get(id).head != null) { queue.offer(users.get(id).head); } } while (!queue.isEmpty() && res.size() < 10) { Tweet tweet = queue.poll(); res.add(tweet.tweetId); if (tweet.next != null) { queue.offer(tweet.next); } } return res; } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { if (!users.containsKey(followerId)) { users.put(followerId, new User(followerId)); } if (!users.containsKey(followeeId)) { users.put(followeeId, new User(followeeId)); } users.get(followerId).follow(followeeId); } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ public void unfollow(int followerId, int followeeId) { if (users.containsKey(followerId) && users.containsKey(followeeId) && followeeId != followerId) { users.get(followerId).unfollow(followeeId); } } } /** * Your Twitter object will be instantiated and called as such: * Twitter obj = new Twitter(); * obj.postTweet(userId,tweetId); * List<Integer> param_2 = obj.getNewsFeed(userId); * obj.follow(followerId,followeeId); * obj.unfollow(followerId,followeeId); */
评论
发表评论