• leetcode 355 设计推特


    用链表存储用户发送的每一个推特,用堆获取最先的10条动态

    1. class Twitter {
    2. Map> followMap;
    3. //规定最新的放到最后
    4. Map postMap;
    5. //优先队列(堆)
    6. PriorityQueue priorityQueue;
    7. int timeStamp = 0;
    8. int limit = 10;
    9. public Twitter() {
    10. followMap = new HashMap();
    11. postMap = new HashMap<>();
    12. //按照每一个推特发送的时间戳由大到小排布
    13. priorityQueue = new PriorityQueue<>((t1,t2) -> t2.timeStamp - t1.timeStamp);
    14. }
    15. //userId发送推特
    16. public void postTweet(int userId, int tweetId) {
    17. //首先根据postMap来获取userId对应发送到文章
    18. Tweet tweet = postMap.get(userId);
    19. //生成新的tweet
    20. Tweet newTweet = new Tweet(tweetId, timeStamp++, tweet);
    21. postMap.put(userId,newTweet);
    22. }
    23. //根据userId获得自己和关注用户的10条推特,按时间顺序由近到远排序
    24. public List getNewsFeed(int userId) {
    25. //因为每一个用户都有自己的优先队列,所以先清空优先队列
    26. priorityQueue.clear();
    27. //将自己和关注的用户发送的最新的推特id先放入到优先队列
    28. if (postMap.containsKey(userId))
    29. priorityQueue.offer(postMap.get(userId));
    30. Set follows = followMap.get(userId);
    31. if (follows != null){
    32. for (Integer follow : follows) {
    33. if (postMap.containsKey(follow))
    34. priorityQueue.offer(postMap.get(follow));
    35. }
    36. }
    37. //现在用户和所有关注的推特都已经放入到优先队列,开始获取前10条
    38. int count = 0;
    39. ArrayList result = new ArrayList<>();
    40. while (!priorityQueue.isEmpty() && count < limit){
    41. //获取头部,在优先队列中删除
    42. Tweet tweet = priorityQueue.poll();
    43. result.add(tweet.id);
    44. if (tweet.next != null)
    45. priorityQueue.offer(tweet.next);
    46. count++;
    47. }
    48. return result;
    49. }
    50. //关注
    51. public void follow(int followerId, int followeeId) {
    52. // 被关注人不能是自己
    53. if (followeeId == followerId) {
    54. return;
    55. }
    56. Set follows = followMap.getOrDefault(followerId, new HashSet<>());
    57. follows.add(followeeId);
    58. followMap.put(followerId,follows);
    59. }
    60. //取关
    61. public void unfollow(int followerId, int followeeId) {
    62. // 被关注人不能是自己
    63. if (followeeId == followerId) {
    64. return;
    65. }
    66. Set follows = followMap.getOrDefault(followerId, new HashSet<>());
    67. follows.remove(followeeId);
    68. followMap.put(followerId,follows);
    69. }
    70. }
    71. class Tweet{
    72. int id;
    73. int timeStamp;
    74. Tweet next;
    75. public Tweet(int id, int timeStamp) {
    76. this.id = id;
    77. this.timeStamp = timeStamp;
    78. }
    79. public Tweet(int id, int timeStamp, Tweet next) {
    80. this.id = id;
    81. this.timeStamp = timeStamp;
    82. this.next = next;
    83. }
    84. }
    85. /**
    86. * Your Twitter object will be instantiated and called as such:
    87. * Twitter obj = new Twitter();
    88. * obj.postTweet(userId,tweetId);
    89. * List param_2 = obj.getNewsFeed(userId);
    90. * obj.follow(followerId,followeeId);
    91. * obj.unfollow(followerId,followeeId);
    92. */

  • 相关阅读:
    TRC 链格孢菌毒素和基因毒素丨艾美捷 TRC Alternariol 9-龙胆二糖苷
    2022-6-30 随机过程之条件期望及其性质 (二)
    简单明了的体会构建者模式
    01--nginx基础
    【数据结构】栈的模拟和使用理解
    AcWing-C/C++语法基础【合集2】
    03UE4 C++ 入门【世界坐标系与局部坐标系】
    pinctrl子系统 - 源码解析(五)
    centos安装mysql8
    EDA工具开发中的调参方法
  • 原文地址:https://blog.csdn.net/hanhanhanha1/article/details/132448929