-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path355.design_twitter.cpp
117 lines (103 loc) · 3.35 KB
/
355.design_twitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//coding:utf-8
/*****************************************
Program: Design tweeter
Description:
Author: [email protected]
Date: 2016-08-17 09:00:37
Last modified: 2016-08-29 16:56:50
GCC version: 4.9.3
*****************************************/
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef struct tweet {
int id;
int timeline;
tweet() {}
tweet(int i, int j): id(i), timeline(j) {}
} TWEET;
typedef struct User {
int id;
vector<TWEET> tweets;
unordered_set<int> to;
unordered_set<int> from;
} USER;
namespace std {
template <>
struct hash<USER>{
size_t operator()(const USER& u) const{
return hash<int>()(u.id);
}
};
}
bool static compare(const TWEET& t1, const TWEET& t2) {
return t1.timeline > t2.timeline;
}
class Twitter {
unordered_map<int, USER> users;
int timeline;
public:
/** Initialize your data structure here. */
Twitter() {
timeline = 0;
}
/** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
if(users.find(userId) == users.end()) {
USER u;
u.id = userId;
u.tweets.push_back(TWEET(tweetId, timeline++));
users[userId] = u;
} else {
users[userId].tweets.push_back(TWEET(tweetId, timeline++));
}
}
/** 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) {
if(users.find(userId) == users.end())
return vector<int>();
vector<TWEET> ret;
int cnt = 0;
for(unordered_set<int>::iterator u = users[userId].to.begin(); u != users[userId].to.end(); u++) {
cnt = 0;
for(int i = users[*u].tweets.size() - 1; i >= 0 && cnt < 10; --i) {
ret.push_back(users[*u].tweets[i]);
cnt++;
}
}
cnt = 0;
for(int i = users[userId].tweets.size() - 1; i >= 0 && cnt < 10; --i) {
ret.push_back(users[userId].tweets[i]);
cnt++;
}
sort(ret.begin(), ret.end(), compare);
vector<int> res;
unordered_set<int> st;
for(int i = 0; i < ret.size() && res.size() < 10; ++i)
if(st.find(ret[i].id) == st.end()) {
st.insert(ret[i].id);
res.push_back(ret[i].id);
}
return res;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
users[followerId].to.insert(followeeId);
users[followeeId].from.insert(followerId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
users[followerId].to.erase(followeeId);
users[followeeId].from.erase(followerId);
}
};
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* vector<int> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/