355. Design Twitter


/*
整个数据是一个map,key是userid,value是一个user对象
user中有自己的fans, 
    follows
    有自己的消息链
取前10个
    浏览自己的fans表 (自己是自己的fans)
    把每个fans的消息入堆
    然后从堆中找出10个最新的。
*/
typedef struct user user_t;
typedef struct news news_t;
struct news
{
int news_id;
int timestamp;
news_t* next;
news(int id, int times) :news_id(id), timestamp(times) { next = NULL; };
};
struct user
{
unordered_set<int>  follows;
news_t*             head;
user() { head = NULL; };
};
class Twitter {
private:
int time;
unordered_map<int, user*>  m_users;
public:
/** Initialize your data structure here. */
Twitter() {
time = 0;
}


/** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
if (!m_users.count(userId))
{
user_t* one_user = new user_t();
one_user->follows.insert(userId);  //flow itself;
m_users[userId] = one_user;
}


user_t*  u = m_users[userId];
news_t*  one_news = new news_t(tweetId, time++);
one_news->next = u->head;
//u->head->next = one_news;
u->head = one_news;


}


/** 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;
if (!m_users.count(userId)) return ans;


auto my_compare = [](const news_t a, const news_t b) {
return a.timestamp < b.timestamp;
};



priority_queue<news_t,vector<news_t>,decltype(my_compare)> pq(my_compare);
for(auto uid:m_users[userId]->follows)
{
            user_t* one_user = m_users[uid];
            news_t*  one_news = one_user->head;
            while(one_news)
            {
                pq.push(*one_news);
                one_news = one_news->next;
            }
}
int count = 0;
while(!pq.empty())
{
    ++count;
            ans.push_back(pq.top().news_id);pq.pop();
            if(count == 10) break;
}

return ans;
}


/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
if (!m_users.count(followerId))
{
user_t* one_user = new user_t();
one_user->follows.insert(followerId);  //flow itself;
m_users[followerId] = one_user;
}
        if (!m_users.count(followeeId))
{
user_t* one_user = new user_t();
one_user->follows.insert(followeeId);  //flow itself;
m_users[followeeId] = one_user;
}
        
user*  u = m_users[followerId];
u->follows.insert(followeeId);
}


/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
        if(followerId ==followeeId ) return;
if (!m_users.count(followerId))
{
user_t* one_user = new user_t();
one_user->follows.insert(followerId);  //flow itself;
m_users[followerId] = one_user;
}
        if (!m_users.count(followeeId))
{
user_t* one_user = new user_t();
one_user->follows.insert(followeeId);  //flow itself;
m_users[followeeId] = one_user;
}
user*  u = m_users[followerId];
u->follows.erase(followeeId);
}
};

猜你喜欢

转载自blog.csdn.net/bjzhaoxiao/article/details/80264296
355