C++ | Leetcode C++题解之第355题设计推特

ops/2024/10/11 7:34:57/

题目:

题解

class Twitter {struct Node {// 哈希表存储关注人的 Idunordered_set<int> followee;// 用链表存储 tweetIdlist<int> tweet;};// getNewsFeed 检索的推文的上限以及 tweetId 的时间戳int recentMax, time;// tweetId 对应发送的时间unordered_map<int, int> tweetTime;// 每个用户存储的信息unordered_map<int, Node> user;
public:Twitter() {time = 0;recentMax = 10;user.clear();}// 初始化void init(int userId) {user[userId].followee.clear();user[userId].tweet.clear();}void postTweet(int userId, int tweetId) {if (user.find(userId) == user.end()) {init(userId);}// 达到限制,剔除链表末尾元素if (user[userId].tweet.size() == recentMax) {user[userId].tweet.pop_back();}user[userId].tweet.push_front(tweetId);tweetTime[tweetId] = ++time;}vector<int> getNewsFeed(int userId) {vector<int> ans; ans.clear();for (list<int>::iterator it = user[userId].tweet.begin(); it != user[userId].tweet.end(); ++it) {ans.emplace_back(*it);}for (int followeeId: user[userId].followee) {if (followeeId == userId) continue; // 可能出现自己关注自己的情况vector<int> res; res.clear();list<int>::iterator it = user[followeeId].tweet.begin();int i = 0;// 线性归并while (i < (int)ans.size() && it != user[followeeId].tweet.end()) {if (tweetTime[(*it)] > tweetTime[ans[i]]) {res.emplace_back(*it);++it;} else {res.emplace_back(ans[i]);++i;}// 已经找到这两个链表合起来后最近的 recentMax 条推文if ((int)res.size() == recentMax) break;}for (; i < (int)ans.size() && (int)res.size() < recentMax; ++i) res.emplace_back(ans[i]);for (; it != user[followeeId].tweet.end() && (int)res.size() < recentMax; ++it) res.emplace_back(*it);ans.assign(res.begin(),res.end());}return ans;}void follow(int followerId, int followeeId) {if (user.find(followerId) == user.end()) {init(followerId);}if (user.find(followeeId) == user.end()) {init(followeeId);}user[followerId].followee.insert(followeeId);}void unfollow(int followerId, int followeeId) {user[followerId].followee.erase(followeeId);}
};

http://www.ppmy.cn/ops/107243.html

相关文章

SQL进阶技巧:如何取时间序列最新完成状态的前一个状态并将完成状态的过程进行合并?

目录 0 问题描述 1 数据准备 2 问题分析 问题1:取最新完成状态的前一个状态 方法1:分析函数求解 方法2:关联求解 问题2:如何将完成状态的过程合并 方法1:分析函数作为辅助变量 方法2:自关联形式获取全量结果集 3 小结 0 问题描述 表status 字段及内容如下:…

【Linux入门】shell基础篇——免交互脚本以及配置实例

文章目录 免交互交互与免交互Here Document 免交互总结 expect工具1. 介绍2. 安装3. 基本概念4. 基本命令4.1 脚本解释器4.2 spawn4.3 expect4.4 send4.5 结束符4.6 set4.7 exp_continue4.8 send_user4.9 接收参数 5. 示例脚本 免交互配置示例修改用户密码脚本 passwd.shsu 切换…

ARM32开发——GD32F4 DMA功能查询

&#x1f3ac; 秋野酱&#xff1a;《个人主页》 &#x1f525; 个人专栏:《Java专栏》《Python专栏》 ⛺️心若有所向往,何惧道阻且长 文章目录 DMA0DMA1 DMA0 DMA1

传统CV算法——基于Opencv的图像绘制

直线绘制 参数解析&#xff1a; &#xff08;图像矩阵&#xff0c;直线起始坐标&#xff0c; 直线终止坐标、颜色、线条厚度&#xff09; cv2.line()是OpenCV中用于绘制直线的函数。 参数说明&#xff1a;img&#xff1a;要绘制直线的图像矩阵。(100,30)&#xff1a;直线的起…

vue3实现打飞机(雷电)

代码可直接运行直接玩&#xff0c;而且要自己加上一些随机事件都很简单了&#xff08;例如发射速度变快&#xff0c;子弹变大&#xff0c;敌人变慢等&#xff09; <template><div class"flex items-center justify-center h-100vh w-full"><div>S…

分享 | 某省级城商行用零信任破解远程访问安全风险

银行建设了各种各样的业务应用系统&#xff0c;不断拓展金融服务的渠道和场景&#xff0c;对远程访问和身份验证的的要求越来越高。零信任架构以其“持续验证、永不信任”的特性&#xff0c;确保只有经过严格身份验证的用户和设备才能访问银行的内部资源&#xff0c;受到了银行…

2024年AI芯片峰会——边缘端侧AI芯片专场

概述 正文 存算一体&#xff0c;解锁大模型的边端侧潜力——信晓旭 当下AI芯片的亟需解决的问题 解决内存墙问题的路径 产品 面向大模型的国产工艺边缘AI芯片创新与展望——李爱军 端侧AI应用“芯”机遇NPU加速终端算力升级——杨磊 边缘端的大模型参数量基本小于100B AI OS…

【如何有效率地阅读源码】

文章目录 前言阅读源码是一项复杂且耗时的任务&#xff0c;但通过一些有效的方法和技巧&#xff0c;可以提高效率和理解度。下面将介绍如何有效率地阅读源码&#xff1a; 一、准备工作二、工具与环境三、逐步深入四、记录与总结五、实践与应用六、持续学习总结 前言 阅读源码是…