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. Difference between web server ,web container and application server

    In Java: Web Container or Servlet Container or Servlet Engine : is used to manage the components lik ...

  2. Lua查找表元素过程(元表、__index方法是如何工作的)

    近日开始研究Lua,在元表的使用上照猫画虎地搞了两下,实现了“面向对象”,但究其本质却略有不解,后咨询牛哥得解,特此记录. Lua的表本质其实是个类似HashMap的东西,其元素是很多的Key-Val ...

  3. RabbitMQ在window的搭建

    RabbitMq window 搭建设置过程,网上有些说的不太明白,所以亲自操刀测试过程,参考了很多人的资料,多谢各位大神的宝贵资料第一步:装RabbitMq运行环境,类似一个虚拟机的东东 1.otp ...

  4. 数据结构与算法分析-AVL树

    1.AVL树是带有平衡条件的二叉查找树. 2.AVL树的每个节点高度最多相差1. 3.AVL树实现的难点在于插入或删除操作.由于插入和删除都有可能破坏AVL树高度最多相差1的特性,所以当特性被破坏时需 ...

  5. C++学习笔记26:泛型编程概念

    一.什么是泛型编程? 泛型就是通用的型式 编写不依赖数据对象型式的代码就是泛型编程 二.为什么需要泛型编程? 函数重载,相似类定义与型式兼容性 例如:设计函数,求两个数据对象的较小值 //未明确规定参 ...

  6. The 11 advantages of Java -Why you choose this language

    Java is never just a language.There are lots of programming languages out there, and few of them mak ...

  7. Project Woosah Tu (五色土)

    I bought this Raspberry Pi (model B) in spring 2013, I hadn't done too much with it except for some ...

  8. 小米note3,华为手机,软键盘弹出之后,页面上定位的元素布局会乱掉

    原因:可能是因为,软键盘弹出时,改变了height,使height:100%,不能达到原来的高度. 解决办法: $(document).ready(function () { $('body').he ...

  9. UNITY更新到5后设置的动画无法播放了怎么办

    点击对应的animator,将 apply root motion  这个选项的勾去掉就可以了,纠结了很久最后在UNITY官方论坛找到的答案

  10. 1 error C4996: 'pcl::SAC_SAMPLE_SIZE':

    使用PCL1.8   中使用粗配准拼接 错误 1 error C4996: 'pcl::SAC_SAMPLE_SIZE': This map is deprecated and is kept onl ...