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 ...
 
随机推荐
- 调试python程序
			
pdb 关键步骤 python -m pdb ***.py n 单步
 - iOS UIPickerView 显示全国省市
			
效果图 #import "ViewController.h" @interface ViewController () @property(strong,nonatomic)UIP ...
 - Python-Day3知识点——深浅拷贝、函数基本定义、内置函数
			
一.深浅拷贝 import copy #浅拷贝 n1={'k1':'wu','k2':123,'k3':['carl',852]} n2=n1 n3=copy.copy(n1) print(id(n1 ...
 - Android 中onSaveInstanceState和onRestoreInstanceState学习
			
1. 基本作用: Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate().onPaus ...
 - The Swift Programming Language 英文原版官方文档下载
			
The Swift Programming Language 英文原版官方文档下载 今天Apple公司发布了新的编程语言Swift(雨燕)将逐步代替Objective-C语言,大家肯定想学习这个语言, ...
 - SQLALchemy(连表)、paramiko
			
本节内容:
 - eclipse常用插件
			
1. eclipse安装主题插件:http://www.eclipsecolorthemes.org/ 2. eclipse terminal插件:在eclipse中集成终端,使用非常方便,不用单独打 ...
 - Bullet的学习资源(用Doxygen生成API文档)
			
Bullet 全称 Bullet Physics Library,是著名的开源物理引擎(可用于碰撞检测.刚体模拟.可变形体模拟),这里将bullet的学习资源整理一下,希望能帮助入门者少走弯路. 看下 ...
 - CSS中的text-overflow:clip|ellipsis的使用
			
如果想让某个容器(div或者li或者...块级元素)显示一行文字,当文字内容过多时,不换行,而是出现...,可以使用text-overflow:clip|ellipsis 基本语法:text-over ...
 - commons-httpclient中的超时设置
			
connectionTimeout与soTimeout的差异,前者指创建一个有效的客户端到服务端链接的最大允许时间,后者指socket接收data的时间. connectionManager.getP ...