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