Skip to content

Latest commit

 

History

History
91 lines (82 loc) · 3.32 KB

355.md

File metadata and controls

91 lines (82 loc) · 3.32 KB

一个不熟悉cpp语法的人强用STL的艰辛之旅

vector<pair<int,int>> 与 pair<vector, vector>的选择 set的insert erase map的find 与迭代器用法

强行用lambda函数当comparator的辛酸 http://stackoverflow.com/questions/5807735/c-priority-queue-with-lambda-comparator-error 附上这个以后研究..

暂时没想到啥了... 类型复杂了就容易错... 以后学下typedef吧

''' class Twitter { map<int, vector<pair<int,int>>> data; map<int, set > followw; int timestamp; public: /** Initialize your data structure here. */ Twitter() { timestamp = 0; }

/** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
    if(data.find(userId) == data.end()) {
        vector<pair<int, int> > a;
        a.push_back(pair<int,int>(tweetId, timestamp++));
        data[userId] = a;
    }
    else {
        data[userId].push_back(pair<int,int>(tweetId, timestamp++));
    }
}

/** 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;
    follow(userId, userId);
    set<int> f = followw[userId];
    priority_queue<pair<pair<int, int>, pair<int, int>>, vector<pair<pair<int, int>, pair<int, int>>>, function<bool(pair<pair<int, int>, pair<int, int>>,pair<pair<int, int>, pair<int, int>>)>> heap([](pair<pair<int, int>, pair<int, int>> a, pair<pair<int, int>, pair<int, int>> b)->bool{return a.first.second < b.first.second;});
    for(auto i = f.begin(); i != f.end(); ++i){
        if(data.find(*i) == data.end())
            continue;
        pair<pair<int, int>, pair<int, int>> a;
        int num = data[*i].size()-1;
        a.first = data[*i][num];
        a.second = pair<int, int>(*i, num);
        heap.push(a);
    }
    int i = 0;
    while(!heap.empty() && i<10){
        auto tmp = heap.top();
        heap.pop();
        ans.push_back(tmp.first.first);
        i++;
        int j = tmp.second.first, k = tmp.second.second-1;
        if(k < 0) continue;
        tmp.first = data[j][k];
        tmp.second = pair<int, int>(j,k);
        heap.push(tmp);
    }
    return ans;
}

/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
    if(followw.find(followerId) == followw.end()){
        followw[followerId] = set<int>();
        followw[followerId].insert(followerId);
    }
    followw[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(followw.find(followerId) == followw.end() || followerId == followeeId)
        return;
    followw[followerId].erase(followeeId);
}

};

/**

  • Your Twitter object will be instantiated and called as such:
  • Twitter obj = new Twitter();
  • obj.postTweet(userId,tweetId);
  • vector param_2 = obj.getNewsFeed(userId);
  • obj.follow(followerId,followeeId);
  • obj.unfollow(followerId,followeeId); */ '''