Mini Twitter
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);
public class Twitter {
int feedMaxNum;
Map<Integer, Set<Integer>> followees;
Map<Integer, List<Tweet>> tweets;
private static int timeStamp = ;
public Twitter() {
feedMaxNum = ;
followees = new HashMap<>();
tweets = new HashMap<>();
}
public void postTweet(int userId, int tweetId) {
if (!tweets.containsKey(userId)) {
tweets.put(userId, new LinkedList<Tweet>());
}
tweets.get(userId).add(, new Tweet(tweetId, Twitter.timeStamp++)); // add new tweet on the first place
}
/**
* 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> feedHeap = new PriorityQueue<>((t1, t2) -> Integer.compare(t1.timePosted, t2.timePosted));
Set<Integer> myFollowees = followees.getOrDefault(userId, new HashSet<>());
myFollowees.add(userId);
for (int followeeId : myFollowees) {
List<Tweet> followeeTweets = tweets.get(followeeId);
if (followeeTweets == null) continue;
for (Tweet t : followeeTweets) {
feedHeap.offer(t);
if (feedHeap.size() > feedMaxNum)
feedHeap.poll();
}
}
List<Integer> myFeed = new LinkedList<>();
while (!feedHeap.isEmpty()) {
myFeed.add(, feedHeap.poll().tweetId);
}
return myFeed;
}
public void follow(int followerId, int followeeId) {
if (!followees.containsKey(followerId))
followees.put(followerId, new HashSet<Integer>());
followees.get(followerId).add(followeeId);
}
public void unfollow(int followerId, int followeeId) {
if (followees.containsKey(followerId)) {
followees.get(followerId).remove(followeeId);
}
}
}
class Tweet {
int tweetId;
int timePosted;
public Tweet(int tId, int time) {
tweetId = tId;
timePosted = time;
}
}
Mini Twitter的更多相关文章
- [LintCode] Mini Twitter 迷你推特
Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text). Post a twe ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 移动端折腾国外分享(facebook、twitter、linkedin)
一.前言 国内做HTML5页面,关注最多就是微信分享了,之前也写过关于微信分享的文章,可以点击查看:分享相关文章 再者,就是国内的其它分享,比如常用的新浪微博.腾讯微博.QQ空间等等,最方便的就是直接 ...
- 电子产品使用感受之--Mac Mini 买了之后有什么用?-- 开发啊!
2019.01.29 更新 Mac Mini 2018这么强劲的性能,不用来做点儿什么真是可惜了. 如果只是用来看看Youtube视频,打开网页看看twitter什么的,那可真是巨大的浪费了. 因为这 ...
- opera mini 7.5安卓改服版
opera mini 7.5安卓改服版Opera mini 7.5安卓版前两天发布了,试着进行改服实现***,过程跟以前的OPM7.0差不多,大家可参照我之前的博客教程Opera mini7.0改服教 ...
- 让MacBook识别noppoo mini 84
让MacBook识别noppoo mini 84 noppoo默认是没有mac驱动的,需要下载一个IOUSBHIDDriverDescriptorOverride否则无法noppoo的键位是识别错误的 ...
- [转]Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
本文转自:https://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-o ...
- 中大东校小米路由器mini实现inode上网,ipv6 wifi【中大】【东校】【inode】【ipv6】
还有不到4个月就要毕业了,前几天半夜没事捣鼓小米路由没想到竟然实现了wifi的ipv6. 正好又安利了同学一台小米路由mini,从刷机到inode到ipv6全搞了一遍. 这里将教程写出来,服务学弟妹. ...
- [LeetCode] Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
随机推荐
- 网络助手的NABCD分析
我们小组这次做的软件名字叫为校园网络助手.本校校园网分为内网与外网认证两种,并且有着流量限制,所以我们设计出来了这项软件,它主要有着两项功能:一键WIFI与校内网盘. N--need.在学校里每当流量 ...
- mysql左外连接
左外连接的概念性不说了,这次就说一说两个表之间的查询步骤是怎么样的? 例如 SELECT ut.id,ut.name,ut.age, ut.sex,ut.status,st.score,st.subj ...
- ESXi 系统管理员命令 [转帖]
来源 https://blog.csdn.net/zy_strive_2012/article/details/53336000 正如所有的虚拟化管理员都知道,要应对VMware基础设施上需要的更多虚 ...
- Docker in Docker的安装 路不通
1. 先启动centos 镜像 然后 再docker cp文件 然后再执行安装报错 [root@CentOS75 ~]# docker run -it centos /bin/bash [root@1 ...
- dx、aapt工具
1.DX命令: Dalvik虚拟机不能直接运行Java代码编译出来的文件,只能运行.dex文件,所以需要将.class,.jar等文件通过DX工具转换成.dex文件. 2.AAPT命令 一个Andro ...
- 从0在windows上一次性上传本地整个项目(包含所有文件/文件夹)到 Github
1.注册并登陆Github. 2.登陆进去之后的页面,点击这个“库”,这表示你在Github上上的代码仓库,我这里已经创建过一个了,所以数量是1 3.在仓库选项卡中,点击“新建”按钮添加一个项目. 4 ...
- 一本通1623Sherlock and His Girlfriend
1623:Sherlock and His Girlfriend 时间限制: 1000 ms 内存限制: 524288 KB [题目描述] 原题来自:Codeforces Round ...
- 洛谷SP16549 QTREE6 - Query on a tree VI(LCT)
洛谷题目传送门 思路分析 题意就是要维护同色连通块大小.要用LCT维护子树大小就不说了,可以看看蒟蒻的LCT总结. 至于连通块如何维护,首先肯定可以想到一个很naive的做法:直接维护同色连通块,每次 ...
- 【bzoj3994】 SDOI2015—约数个数和
http://www.lydsy.com/JudgeOnline/problem.php?id=3994 (题目链接) 题意 多组询问,给出${n,m}$,求${\sum_{i=1}^n\sum_{j ...
- 在c语言中嵌入汇编语句,对于我来说相当难。
今天早上在csdn论坛上看到一个帖子http://topic.csdn.net/u/20120917/14/82f42e17-977a-4824-95bd-7b79db15d283.html:“C语言 ...