Skip to content

Commit 897166a

Browse files
committed
add 355 Design Twitter
1 parent 1468df3 commit 897166a

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

Combination/355_DesignTwitter.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @Author: xuezaigds@gmail.com
3+
* @Last Modified time: 2016-07-08 15:10:06
4+
*/
5+
6+
class Twitter {
7+
struct Tweet
8+
{
9+
int time;
10+
int id;
11+
Tweet(int time, int id) : time(time), id(id) {}
12+
};
13+
14+
struct compare{
15+
bool operator()(const Tweet x, const Tweet y) {
16+
return x.time < y.time;
17+
}
18+
};
19+
20+
int time;
21+
unordered_map<int, vector<Tweet>> tweets;
22+
unordered_map<int, unordered_set<int>> followees;
23+
24+
public:
25+
/** Initialize your data structure here. */
26+
Twitter(): time(0) {
27+
}
28+
29+
/** Compose a new tweet. */
30+
void postTweet(int userId, int tweetId) {
31+
tweets[userId].push_back(Tweet(time++, tweetId));
32+
}
33+
34+
/** Retrieve the 10 most recent tweet ids in the user's news feed.
35+
Each item in the news feed must be posted by users who the user
36+
followed or by the user herself.
37+
Tweets must be ordered from most recent to least recent. */
38+
vector<int> getNewsFeed(int userId) {
39+
priority_queue<Tweet, vector<Tweet>, compare> allnews;
40+
for(auto u: followees[userId]){
41+
for(auto &t: tweets[u]){
42+
allnews.push(t);
43+
}
44+
}
45+
for(auto &t: tweets[userId]){
46+
allnews.push(t);
47+
}
48+
vector<int> ans;
49+
for(int i=0; i<10 && !allnews.empty(); i++){
50+
ans.push_back(allnews.top().id);
51+
allnews.pop();
52+
}
53+
return ans;
54+
}
55+
56+
/** Follower follows a followee. If the operation is invalid,
57+
it should be a no-op. */
58+
void follow(int followerId, int followeeId) {
59+
if(followerId != followeeId){
60+
followees[followerId].insert(followeeId);
61+
}
62+
}
63+
64+
/** Follower unfollows a followee. If the operation is invalid,
65+
it should be a no-op. */
66+
void unfollow(int followerId, int followeeId) {
67+
followees[followerId].erase(followeeId);
68+
}
69+
};
70+
71+
/**
72+
* Your Twitter object will be instantiated and called as such:
73+
* Twitter obj = new Twitter();
74+
* obj.postTweet(userId,tweetId);
75+
* vector<int> param_2 = obj.getNewsFeed(userId);
76+
* obj.follow(followerId,followeeId);
77+
* obj.unfollow(followerId,followeeId);
78+
*/
79+
80+
/*
81+
["Twitter","postTweet","postTweet","getNewsFeed","postTweet","getNewsFeed"]
82+
[[],[1,5],[1,3],[3,5],[1,6],[3,5,6]]
83+
["Twitter","postTweet","getNewsFeed","follow","postTweet","getNewsFeed","unfollow","getNewsFeed"]
84+
[[],[1,5],[1],[1,2],[2,6],[1],[1,2],[1]]
85+
*/

Combination/355_DesignTwitter.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Author: xuezaigds@gmail.com
4+
# @Last Modified time: 2016-07-08 13:57:50
5+
6+
7+
class Twitter(object):
8+
"""
9+
Accordting to:
10+
https://discuss.leetcode.com/topic/47838/python-solution
11+
"""
12+
def __init__(self):
13+
self.timer = itertools.count(step=-1)
14+
self.tweets = collections.defaultdict(collections.deque)
15+
self.followees = collections.defaultdict(set)
16+
17+
def postTweet(self, userId, tweetId):
18+
"""Compose a new tweet.
19+
"""
20+
self.tweets[userId].appendleft((next(self.timer), tweetId))
21+
22+
def getNewsFeed(self, userId):
23+
"""Retrieve the 10 most recent tweet ids in the user's news feed.
24+
25+
Each item in the news feed must be posted by users who the user
26+
followed or by the user herself.
27+
Tweets must be ordered from most recent to least recent.
28+
"""
29+
tweets = heapq.merge(*(self.tweets[u] for u in
30+
(self.followees[userId] | {userId})))
31+
return [t for _, t in itertools.islice(tweets, 10)]
32+
33+
def follow(self, followerId, followeeId):
34+
"""Follower follows a followee. If the operation is invalid, it should be a no-op.
35+
"""
36+
self.followees[followerId].add(followeeId)
37+
38+
def unfollow(self, followerId, followeeId):
39+
"""Follower unfollows a followee. If the operation is invalid, it should be a no-op.
40+
"""
41+
self.followees[followerId].discard(followeeId)
42+
43+
44+
# Your Twitter object will be instantiated and called as such:
45+
# obj = Twitter()
46+
# obj.postTweet(userId,tweetId)
47+
# param_2 = obj.getNewsFeed(userId)
48+
# obj.follow(followerId,followeeId)
49+
# obj.unfollow(followerId,followeeId)
50+
51+
"""
52+
["Twitter","postTweet","postTweet","getNewsFeed","postTweet","getNewsFeed"]
53+
[[],[1,5],[1,3],[3,5],[1,6],[3,5,6]]
54+
["Twitter","postTweet","getNewsFeed","follow","postTweet","getNewsFeed","unfollow","getNewsFeed"]
55+
[[],[1,5],[1],[1,2],[2,6],[1],[1,2],[1]]
56+
"""

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@
307307
* 146. [LRU Cache](Combination/146_LRUCache.py)
308308
* 300. [Longest Increasing Subsequence](Combination/300_LongestIncreasingSubsequence.py)
309309
* 329. [Longest Increasing Path in a Matrix](Combination/329_LongestIncreasingPathInMatrix.py)
310+
* 355. [Design Twitter](Combination/355_DesignTwitter.py)
310311

311312
# DFA
312313

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy