第355题---Design Twitter

  • 题意描述

    • 设计一个简答的Twitter,其中包含postTweet(userId, tweetId), getNewFeed(userId), follow(followerId, followeeId), unfollow(followerId, follloweeId)函数
  • 思路

    • 堆和哈希表
  • 代码

    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
    class Twitter {
    struct Tweet {
    int time;
    int id;
    Tweet(int time, int id) : time(time), id(id) {}
    };
    std::unordered_map<int, std::vector<Tweet>> tweets;
    std::unordered_map<int, std::unordered_set<int>> following;
    int time;
    public:
    /** Initialize your data structure here. */
    Twitter() : time(0) {
    }
    /** Compose a new tweet. */
    void postTweet(int userId, int tweetId) {
    tweets[userId].emplace_back(time++, tweetId);
    }
    /** 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) {
    std::vector<std::pair<Tweet*, Tweet*>> h;
    for(auto& u: following[userId]) {
    auto& t = tweets[u];
    if(t.size() > 0)
    h.emplace_back(t.data(), t.data() + t.size() - 1);
    }
    auto& t = tweets[userId];
    if(t.size() > 0)
    h.emplace_back(t.data(), t.data() + t.size() - 1);
    auto f = [](const std::pair<Tweet*, Tweet*>& x, const std::pair<Tweet*, Tweet*>& y) {
    return x.second->time < y.second->time;
    };
    std::make_heap(h.begin(), h.end(), f);
    const int n = 10;
    std::vector<int> o;
    o.reserve(n);
    for(int i = 0; (i < n) && !h.empty(); ++i) {
    std::pop_heap(h.begin(), h.end(), f);
    auto& hb = h.back();
    o.push_back(hb.second->id);
    if(hb.first == hb.second--)
    h.pop_back();
    else
    std::push_heap(h.begin(), h.end(), f);
    }
    return o;
    }
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    void follow(int followerId, int followeeId) {
    if(followerId != followeeId)
    following[followerId].insert(followeeId);
    }
    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    void unfollow(int followerId, int followeeId) {
    following[followerId].erase(followeeId);
    }
    };
    /**
    * 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);
    */
坚持原创技术分享,您的支持将鼓励我继续创作!

热评文章