LeetCode-Design Twitter
2016-07-24 11:30
381 查看
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:
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: 1. getNewsFeed() should be quick. We can store a user's tweets in a list, then in the call of this function, we find out all followed users and essentially do a merge on the most recent 10 tweets of these users (including @userId himself). To make this quick, we can make Tweet class as a link list. So a PriorityQueue<Tweet> should be enough to achieve all the work. Solution:
public class Twitter { private static int timestamp = 0; public class Tweet { int tweetId; int userId; int time; Tweet next; public Tweet(int uid, int tid){ tweetId = tid; userId = uid; time = timestamp++; next = null; } } /** Initialize your data structure here. */ public Twitter() { tweetMap = new HashMap<Integer,Tweet>(); followMap = new HashMap<Integer,HashSet<Integer>>(); } /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { Tweet t = new Tweet(userId,tweetId); t.next = tweetMap.get(userId); tweetMap.put(userId,t); } /** 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) { List<Integer> resList = new LinkedList<Integer>(); PriorityQueue<Tweet> q = new PriorityQueue<Tweet>((a,b) -> (b.time - a.time)); follow(userId,userId); HashSet<Integer> followSet = followMap.get(userId); if (followSet != null){ for (Integer id : followSet) if (tweetMap.containsKey(id)) q.add(tweetMap.get(id)); } while (resList.size()<10 && !q.isEmpty()){ Tweet t = q.poll(); resList.add(t.tweetId); if (t.next!=null) q.add(t.next); } return resList; } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { if (followMap.containsKey(followerId)){ followMap.get(followerId).add(followeeId); } else { HashSet<Integer> set = new HashSet<Integer>(); set.add(followeeId); followMap.put(followerId,set); } } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ public void unfollow(int followerId, int followeeId) { if (followMap.containsKey(followerId)){ followMap.get(followerId).remove(followeeId); } } HashMap<Integer,Tweet> tweetMap; HashMap<Integer,HashSet<Integer>> followMap; } /** * 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); */
相关文章推荐
- (模板题)sdut 3373 数据结构实验之查找一:二叉排序树(二叉排序树的建立)
- 关于HTML学习重点-绝对布局和相对布局总结
- 对于优先队列的一些思考
- poj 1182 并查集高级应用
- poj 2229 (dp 完全背包相似问题)
- PHP函数之trim
- linux atime mtime ctime
- git 常用命令详解
- [置顶] QT操作Word,插入表格
- jsp监听器listener
- hdu 5748 Bellovin【nlogn最长递增子序列】
- 求多边形面积
- java单例设计模式
- 【杭电 1722】Cake
- JS最佳实践
- PHP学习之路之Smarty变量调节器
- 代码的正常分支需要打印log吗?
- Java替换字符串中的空格
- 首页-底部&顶部Tab导航(菜单栏)的实现:TabLayout+ViewPager+Fragment
- 线性表——双链表