题目连接

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的更多相关文章

  1. leetcode@ [355] Design Twitter (Object Oriented Programming)

    https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...

  2. [LeetCode] 355. Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  3. [leetcode]355. Design Twitter设计实现一个微博系统

    //先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...

  4. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  5. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  6. 【LeetCode】355. Design Twitter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【Leetcode】355. Design Twitter

    题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...

  8. LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  9. #Leetcode# 707. Design Linked List

    https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...

随机推荐

  1. 阶段2-新手上路\项目-移动物体监控系统\Sprint2-摄像头子系统开发\第2节-V4L2图像编程接口深度学习

    参考资料: http://www.cnblogs.com/emouse/archive/2013/03/04/2943243.htmlhttp://blog.csdn.net/eastmoon5021 ...

  2. appium+python+jenkins+selenium grid+unnittest+生成报告打造UI自动化回归、监控体系

    先放一下截图,展示一下平台做成的样子,以及实现后的结果,后面贴上自动化用例执行的过程中,帮我们发现的线上问题 关于appium自动化环境的安装,网上有很多教程,我就不重复赘述,后面陆续写出设计思想,贴 ...

  3. jquery的命名空间

    function A( event ){    alert( 'A' );}function B( event ){    alert( 'B' );}function C( event ){    ...

  4. jquery事件之事件处理函数

    一.事件处理 方法名 说明 语法 (events 事件类型,data数据,handler 事件处理函数,selector 选择器) Bind( 为每一个匹配元素的特定事件(像click)绑定一个事件处 ...

  5. C#中的运算符和表达式

    说起C#运算符和表达式,小伙伴们肯定以为很简单,其实要用好表达式,不是一件容易的事.一个好的表达式可以让你做事半功倍的效果,比如三元表达式,可以让你少写N多个if和case语句. 表达式 由 操作数( ...

  6. SpringBoot应用篇(一):自定义starter

    一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...

  7. Codeforces Round #522 Div2C(思维)

    #include<bits/stdc++.h>using namespace std;int a[200007];int b[200007][7];int ans[200007];int ...

  8. Java基础--正则表达式的规则

    注意:正则表达式只关注格式是否正确,不关注内容是否有效. 一.字符集合, []表示一个字符. 1.[abc] :指a,b,c中的任意一个字符. 2.[^abc]:指除了a,b,c外的任意字符. 3.[ ...

  9. 任务计划cron

    在linux中,任务计划分俩:未来时间只执行一次和周期性执行 at:未来时间只执行一次 -V 显示版本信息 -l: 列出指定队列中等待运行的作业:== atq -d: 删除指定的作业:== atrm ...

  10. ELK系列(4) - Elasticsearch cannot write xcontent for unknown value of type class java.math.BigDecimal

    问题与分析 在使用Elasticsearch进行index数据时,发现报错如下: java.lang.IllegalArgumentException: cannot write xcontent f ...