码迷,mamicode.com
首页 > 其他好文 > 详细

355. 设计推特

时间:2020-04-22 23:00:19      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:cout   find   小根堆   关注   empty   usr   while   返回   自己   

 1 class Twitter 
 2 {
 3 public:
 4     unordered_map<int,priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>> u;//用户 -> (出现的次数,推文) 小根堆
 5     unordered_map<int,vector<int>> usr;  //用户 -> 关注的用户
 6     int count = 0;//次数
 7 
 8 public:
 9     /** Initialize your data structure here. */
10     Twitter() {}
11     
12     /** Compose a new tweet. */
13     void postTweet(int userId, int tweetId) 
14     {
15         u[userId].push({++count,tweetId});
16     }
17     
18     vector<int> getNewsFeed(int userId) 
19     {
20         vector<int> res;
21         if(usr[userId].size() == 0)//没有关注任何人
22         {
23             priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> temp = u[userId];
24             while(!temp.empty())
25             {
26                 auto it = temp.top();
27                 temp.pop();
28                 res.insert(res.begin(),it.second);
29             }
30             if(res.size() > 10)
31             {
32                 return vector<int>(res.begin(),res.begin()+10);
33             }
34             return res;
35         }
36 
37         priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
38         vector<int> nums;//所有用户包括自己以及关注人
39         nums.push_back(userId);
40         nums.insert(nums.end(),usr[userId].begin(),usr[userId].end());//{2,1}
41         
42         cout << u[userId].size() << endl;
43 
44         for(auto a : nums)//把所有用户的推文都推到小根堆里面去
45         {
46             cout << a << endl;
47             priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> temp = u[a];
48             while(!temp.empty())
49             {
50                 auto it = temp.top();
51                 temp.pop();
52                 cout << it.first << " " << it.second << endl;
53                 pq.push({it.first,it.second});
54             }
55         }
56 
57         while(!pq.empty())
58         {
59             auto it = pq.top();
60             pq.pop();
61             res.insert(res.begin(),it.second);
62         }
63         if(res.size() > 10)
64         {
65             return vector<int>(res.begin(),res.begin()+10);
66         }
67         return res;
68     }
69     
70     /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
71     void follow(int followerId, int followeeId) 
72     {
73         if(followerId == followeeId) return;//如果两者相等,则直接返回
74         auto it = find(usr[followerId].begin(),usr[followerId].end(),followeeId);
75         if(it == usr[followerId].end())
76         {
77             usr[followerId].push_back(followeeId);
78         }
79     }
80     
81     /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
82     void unfollow(int followerId, int followeeId) 
83     {
84         auto pos = find(usr[followerId].begin(),usr[followerId].end(),followeeId);
85         if(pos != usr[followerId].end())
86         {
87             usr[followerId].erase(pos);
88         }
89     }
90 };

 

355. 设计推特

标签:cout   find   小根堆   关注   empty   usr   while   返回   自己   

原文地址:https://www.cnblogs.com/yuhong1103/p/12757375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!