LeetCode "Design Twitter"
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"的更多相关文章
- [LeetCode] Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- 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 解题报告(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]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...
- [LeetCode] Design Phone Directory 设计电话目录
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- [LeetCode] Design Hit Counter 设计点击计数器
Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
随机推荐
- portotype
[ portotype ] [语法] function :function Name是创建新的函数的名称 body : body可以选项,包含调用该函数时被执行的JScrtipt 代码的字符串. ...
- 在Visual Studio中使用Git命令提示符
VS2015自带了Git插件,但有时候我觉得Git控制台命令更方便些. VS中本身不能把Git Bush作为浮动窗集成进来,但我们可以通过Power Shell来使用Git命令. ---------- ...
- Adobe Dreamweaver代码编辑
Adobe Dreamweaver 同义词 DW(Adobe Dreamweaver的缩写)一般指Adobe Dreamweaver Adobe Dreamweaver,简称“DW”,中文名称 &qu ...
- LINQ To SQL
议程 1.LINQ To SQL概述 2.LINQ To SQL对象模型 3.LINQ To SQL查询 用到的数据库 SQL Server 2005,数据库名为Test. 两张表,分别为Studen ...
- win32自绘按钮,使用GDI+(二)
一.解决上一篇的两个问题. 1.按钮背景透明 方法是,在绘制按钮之前,向按钮的父窗口发生WM_CTLCOLORBTN消息.该消息返回一个画刷句柄,系统使用该画刷句柄画出按钮的背景.所以我们在处理这个消 ...
- Linux使用mount挂载samba共享
# 挂载smb使用读写777权限sudo mount -t cifs -o "rw,dir_mode=0777,file_mode=0777,username=winuser,passwor ...
- iOS后台挂起程序 当程序到后台后,继续完成Long-Running Task 任务
我们知道,到我们程序从前台退到后台(安home)键后,将执行程序的委托方法. // 当应用程序掉到后台时,执行该方法 - (void)applicationDidEnterBackground:(UI ...
- FZU 1759 欧拉函数 降幂公式
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...
- android 底层入门开发(二)
LED将为我闪烁:控制发光二极管 对于大多数Linux驱动来说,需要直接与硬件交互,本章主要介绍用Linux驱动来控制二极管的明暗,即通过Linux驱动发送数据控制开发板上LED灯的开关. 第一节介绍 ...
- Clojure web初探
项目环境:3.2.0-52-generic #78-Ubuntu SMP Fri Jul 26 16:21:44 UTC 2013 x86_64 x86_64 x86_64 GNU/LinuxLein ...