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. tab使用 TabActivity TabHost Tabspec常用方法

    本文是参考Android官方提供的sample里面的ApiDemos的学习总结.   TabActivity   首先Android里面有个名为TabActivity来给我们方便使用.其中有以下可以关 ...

  2. Inversion_树状数组***

    Problem Description You have a sequence {a1,a2,...,an} and you can delete a contiguous subsequence o ...

  3. Excel中如何过滤复选框 How to filter checkbox column in Excel

    有个笨笨的小伙伴最近老跟Excel过不去. 她新加了一列checkbox,还想filter别人有没有选.写代码又不会,怎么办. 1. 首先,加这个checkbox 2. 然后新建一列,把这个check ...

  4. Keepalived双机热备

    一,Keepalived双机热备的应用场景 1,网站流量不高,压力不大,但是对服务器的可靠性要求极其高,例如实时在线OA系统,政府部门网站系统,医院实时报医系统,公安局在线报案系统,股市后台网站系统等 ...

  5. Python为什么要self

    Python要self的理由 Python的类的方法和普通的函数有一个很明显的区别,在类的方法必须有个额外的第一个参数 (self ),但在调用这个方法的时候不必为这个参数赋值 (显胜于隐 的引发). ...

  6. SILVERLIGHT 应急卫生模拟演练项目之loading界面实现

    第一次在博客园写文章 俺是菜鸟 有不足之处还请大佬们多多指教 第一次也不知道该写啥 俺就拿自己最近做的一个项目 来细说吧 俺们公司是做医疗卫生方面的  其中有一块涉及到应急卫生模拟演练方面 这块分到我 ...

  7. Advanced SystemCare 系统优化软件

    这是一款国外非常流行的系统优化软件,功能很全面,而且中文版用起来也没有语言障碍. Pro版: C0184-8F31F-4B337-296F7 8DE4A-352D9-B4531-D60F7 5B91B ...

  8. 《C与指针》第十五章练习

    本章例程 15.1打开和关闭文件 #include <stdlib.h> #include <stdio.h> int main(int ac, char **av) { in ...

  9. 解决java使用https协议请求出现证书不信任问题(PKIX path building failed)

    解决https请求时出现pkix path building fail错误 方法 将submail.cer 安全证书导入到java中的cacerts证书库 (sumail是我从https://api. ...

  10. qsort C++ VS2013 leetcode

    class Solution { private: static int compare(const void * a, const void * b) { return (*(int*)a - *( ...