leetcode 355 Design Twitte
题目连接
https://leetcode.com/problems/design-twitter
Design Twitte
Description
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:
1. postTweet(userId, tweetId): Compose a new tweet.
2. 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.
3. follow(followerId, followeeId): Follower follows a followee.
4. 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);
根据题意直接模拟即可,
我用了一个固定大小的set来保存10个最近的id
如果tweets少于10个直接插入即可,否则每插入一个就与set的最后一个元素判断
(已重载比较函数,set里面的元素是以时间戳从大到小排序的)
PS:好久都没有更新blogs了,太懒了(⊙﹏⊙)b
class Twitter {
private:
struct P {
int id, ref;
P(int i = 0, int j = 0) :id(i), ref(j) {}
inline bool operator<(const P &a) const { return ref > a.ref; }
};
public:
Twitter() { time = 1; }
void postTweet(int userId, int tweetId) {
userPost[userId].insert(P(tweetId, time++));
userFollow[userId].insert(userId);
}
vector<int> getNewsFeed(int userId) {
q.clear();
vector<int> res;
for(auto &r: userFollow[userId]) {
int n = userPost[r].size();
auto it = userPost[r].begin();
n = min(n, 10);
while(n--) {
if(q.size() < 10) {
q.insert(*it++);
} else {
auto c = q.end();
if(*it < *--c) {
q.erase(c);
q.insert(*it++);
}
}
}
}
for(auto &r: q) res.push_back(r.id);
return res;
}
void follow(int followerId, int followeeId) {
userFollow[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
if(followerId == followeeId) return;
userFollow[followerId].erase(followeeId);
}
private:
int time;
set<P> q;
unordered_map<int, set<P>> userPost;
unordered_map<int, set<int>> userFollow;
};
leetcode 355 Design Twitte的更多相关文章
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- [LeetCode] 355. Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- [leetcode]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
- LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- #Leetcode# 707. Design Linked List
https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...
随机推荐
- 阶段3-团队合作\项目-网络安全传输系统\sprint3-账号管理子系统设计\第2课-账号管理子系统设计
账号管理子系统的设计 客户端需要登录到服务器,在服务器去查询数据库,进行验证该用户. 打开client.c文件 编译之 把它复制到开发板里面去 这个程序是在本地数据库测试的!!!!!!!!!!!!!! ...
- JavaScript: 高级技巧: window 对象也可以添加自定义属性
JavaScript: 高级技巧: window 对象也可以添加自定义属性 例如 window.ntName = 'a';例如 window.ntXw = top; 优点是, window 无须等加载 ...
- C#中IQueryable和IEnumerable的区别
最近的一个面试中,被问到IQueryable 和 IEnumerable的区别, 我自己看了一些文章,总结如下: 1. 要明白一点,IQueryable接口是继承自IEnumerable的接口的. 2 ...
- Django 之 logging
1. logging 1.1 什么是 logging logging 模块是 Python 内置的日志管理模块,不需要额外安装. 使用: import logging logging.critical ...
- superset 错误解决
访问superset localhost:8088 securety->list Role 报错 xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxx ...
- 关于pacemaker监控mysql修复的方法
对工作中,涉及到数据库修复的一个简单汇总 1.在所有的控制节点上,执行pcs resource命令行,查看控制节点上pacemaker的状态是否异常,如果异常,通过crm_resource -P命令行 ...
- linux下oracle一些常用命令
dbca 配置数据库netca 配置tnslsnrctl status tns状态lsnrctl stop TNS停止lsnrctl start TNS启动
- 【Kafka】《Kafka权威指南》入门
发布与订阅消息系统 在正式讨论Apache Kafka (以下简称Kafka)之前,先来了解发布与订阅消息系统的概念, 并认识这个系统的重要性.数据(消息)的发送者(发布者)不会直接把消息发送给接收 ...
- 解决request中文乱码问题
因为request请求都是ISO-8859-1,而jsp页面是采用UTF-8编码,所以当传递的参数有中文时,页面会出现乱码,但是可以将取到的数据通过String的构造函数使用指定的编码类型重新构造一个 ...
- ie-"此更新不适应于此电脑"
cmd-dos命令 expand –F:* C:\update\Windows6.1-KB2533623-x64.msu C:\update\ dism.exe /online /Add-Packag ...