A mix of hashmap, list and heap.

struct Tw
{
Tw(long long pts, int tid)
{
ts = pts;
tweetid = tid;
}
long long ts;
int tweetid;
};
struct Cmp
{
bool operator()(const Tw &a, const Tw &b)
{
return a.ts > b.ts;
}
};
class Twitter {
long long ts;
unordered_map<int, unordered_set<int>> fllw;
unordered_map<int, list<Tw>> twts;
public:
/** Initialize your data structure here. */
Twitter() {
ts = ;
} /** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
twts[userId].push_back({ts ++, tweetId});
if(twts[userId].size() > ) twts[userId].pop_front();
} /** 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. */
vector<int> getNewsFeed(int userId) {
priority_queue<Tw, vector<Tw>, Cmp> q;
for(auto uid : fllw[userId])
{
for(auto &tw : twts[uid])
{
q.push(tw);
if(q.size() > ) q.pop();
}
}
for(Tw &tw : twts[userId])
{
q.push(tw);
if(q.size() > ) q.pop();
} vector<int> ret;
while(!q.empty())
{
ret.push_back(q.top().tweetid);
q.pop();
}
reverse(ret.begin(), ret.end());
return ret;
} /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
if (followerId != followeeId)
fllw[followerId].insert(followeeId);
} /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
fllw[followerId].erase(followeeId);
}
};

LeetCode "Design Twitter"的更多相关文章

  1. [LeetCode] Design Twitter 设计推特

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

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

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

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

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

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

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

  5. 【Leetcode】355. Design Twitter

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

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

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

  7. [LeetCode] Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  8. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  9. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

随机推荐

  1. 第9章 硬件抽象层:HAL

    HAL(硬件抽象层)是建立在Linux驱动之上的一套程序库.这套程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.在传统的Linux系统中Linux驱动一般有两种类型的代码:访问硬件 ...

  2. 【转】Duff's Device

    在看strcpy.memcpy等的实现发现用了内存对齐,每一个word拷贝一次的办法大大提高了实现效率,参加该blog(http://totoxian.iteye.com/blog/1220273). ...

  3. bookstore网上书店测试缺陷报告2

    Bookstore网上书店系统测试缺陷报告   缺陷编号 01.01.0002 发现人 吴赵昕 记录日期 2016-06-10 所属模块 购物车 确认人 吴赵昕 确认日期 2016-06-10 当前状 ...

  4. poj1741 (点分治)

    Problem Tree 题目大意 给一棵树,有边权.求树上距离小于等于K的点对有多少. 解题分析 点分治.对每一棵子树进行dfs,求出每棵子树的重心,继而转化为子问题. 对于经过根的路径i--j,令 ...

  5. HDU 1839

    http://acm.hdu.edu.cn/showproblem.php?pid=1839 题意:从1到n,要求时间小于等于T到达.每条边有一个容量,问最多能运多少货物. 分析:最多能运的货物取决于 ...

  6. MSSQL—行转列

    行转列,是SQL中经常会遇到的一个问题,并且分为静态转换和动态转换,所谓静态转换即在转换的行数已知或固定:动态转换则为转换的行数不固定. 转换的方法一般采用case when语句或pivot(MSSQ ...

  7. MySql数据库忘记root密码

    以windows为例: 1. 关闭正在运行的MySQL服务.(services.msc运行停止服务) 2. 打开DOS窗口,转到mysql\bin目录.(输入cd..返回到c盘根目录下,一般MySQL ...

  8. IOS OC 多任务定时器 NSRunLoop 管理 NSTimer

    下面有两种做法 1.使用日期组件 NSDateComponents 2.使用NSString 生成一个日期 //  创建一个日历对象 NSCalendar *calendar = [NSCalenda ...

  9. Hbuilder与svn快速连接并在手机上测试页面

    大家好,今天讲一下Hbuilder怎样与svn连接在一起,并且在移动端上面做真是的页面测试. 1,打开Hbuilder软件,在工具中,安装插件,找到svn插件安装. 2.点击文件,导入,从svn中检测 ...

  10. Android常用库

    原文链接:http://www.jianshu.com/p/19368c2cdcaf 系统框架 1. 网络请求 Android Async HTTP Android异步HTTP库 AndroidAsy ...