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 ...
随机推荐
- 《精通Spring4.X企业应用开发实战》读后感第五章(FactoryBean)
- JavaScript: 高级技巧: window 对象也可以添加自定义属性
JavaScript: 高级技巧: window 对象也可以添加自定义属性 例如 window.ntName = 'a';例如 window.ntXw = top; 优点是, window 无须等加载 ...
- go语言web开发框架_Iris框架讲解(五):MVC包使用
在Iris框架中,封装了mvc包作为对mvc架构的支持,方便开发者遵循mvc的开发原则进行开发. iris框架支持请求数据.模型.持久数据分层处理,并支持各层级模块代码绑定执行. MVC即:model ...
- 2017-10-6 清北刷题冲刺班a.m
1.角谷猜想 #include<iostream> #include<cstdio> #include<cstring> #define maxn 10010 us ...
- Oracle表连接(转)
表之间的连接 Join是一种试图将两个表结合在一起的谓词,一次只能连接2个表,表连接也可以被称为表关联.在后面的叙述中,我们将会使用”row source”来代替”表”,因为使用row source更 ...
- Spring是一个分层的Java
Spring简介 Spring是一个分层的Java SE/EE应用一站式的轻量级开源框架.Spring核心是IOC和AOP. Spring优点 -方便解耦,简化开发,通过Spring提供的IoC容器, ...
- SQL语法:MySQL系列之四
一.SQL语言的简介和规范 是一种特定目的程序语言,用于管理关系数据库管理系统(RDBMS),或在关系流数据管理系统(RDSMS)中进行流处理. 20世纪70年代,IBM开发出SQL,用于DB2 ...
- 洛谷P2846 光开关Light Switching
题目描述 灯是由高科技--外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右击两个灯所连的鼠 标,你就可以知道这两个灯,以及之间的灯有多少灯是亮的.起初 ...
- ELK系列(4) - Elasticsearch cannot write xcontent for unknown value of type class java.math.BigDecimal
问题与分析 在使用Elasticsearch进行index数据时,发现报错如下: java.lang.IllegalArgumentException: cannot write xcontent f ...
- 1089 Insert or Merge(25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...