-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeWork04_Design_Twitter.cpp
More file actions
105 lines (88 loc) · 2.99 KB
/
HomeWork04_Design_Twitter.cpp
File metadata and controls
105 lines (88 loc) · 2.99 KB
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
struct Node{
int tweetId;
int time;
Node * next;
Node(int tweetId, int time):tweetId(tweetId),time(time),next(nullptr){
}
};
bool operator<(const Node& a, const Node& b){
return a.time < b.time;
}
class Twitter {
public:
/** Initialize your data structure here. */
Twitter() {
time = 0;
userId_Node = vector<Node *>(501, nullptr);
followers = vector<set<int>>(501,set<int>());
}
/** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
++time;
Node* newnode = new Node(tweetId,time);
if(userId_Node[userId] == nullptr){
userId_Node[userId] = newnode;
//cout << tweetId << " " ;
}
else{
Node* ptr = userId_Node[userId];
Node* prev = nullptr;
while(ptr != nullptr){
prev = ptr;
ptr = ptr->next;
}
prev->next = newnode;
}
}
/** 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) {
vector<int> ans;
priority_queue<Node *> top10;
Node* node = userId_Node[userId];
while(node != nullptr ){
top10.push(node);
node = node->next;
}
for(int follow : followers[userId]){
//cout << follow << " ";
node = userId_Node[follow];
while(node != nullptr ){
top10.push(node);
//cout << node->tweetId << " ";
node = node->next;
}
}
for(int i=0; i < 10; i++){
if(top10.empty()) break;
ans.push_back(top10.top()->tweetId);
top10.pop();
}
return ans;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
//follower and followeeid not existed
//if(userId_Node[followerId] == nullptr ) return;
//cout << "heelo";
followers[followerId].insert(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
if( followers[followerId].find(followeeId) == followers[followerId].end()) return;
else{
followers[followerId].erase(followeeId);
}
}
private:
vector<Node*> userId_Node;
vector<set<int>> followers;
int time;
};
/**
* 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);
*/