355. 设计推特
题目描述
设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。你的设计需要支持以下的几个功能:
- postTweet(userId, tweetId): 创建一条新的推文
- getNewsFeed(userId): 检索最近的十条推文。每个推文都必须是由此用户关注的人或者是用户自己发出的。推文必须按照时间顺序由最近的开始排序。
- follow(followerId, followeeId): 关注一个用户
- unfollow(followerId, followeeId): 取消关注一个用户
示例
Twitter twitter = new Twitter();
// 用户1发送了一条新推文 (用户id = 1, 推文id = 5).
twitter.postTweet(1, 5);
// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
twitter.getNewsFeed(1);
// 用户1关注了用户2.
twitter.follow(1, 2);
// 用户2发送了一个新推文 (推文id = 6).
twitter.postTweet(2, 6);
// 用户1的获取推文应当返回一个列表,其中包含两个推文,id分别为 -> [6, 5].
// 推文id6应当在推文id5之前,因为它是在5之后发送的.
twitter.getNewsFeed(1);
// 用户1取消关注了用户2.
twitter.unfollow(1, 2);
// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
// 因为用户1已经不再关注用户2.
twitter.getNewsFeed(1);
解题思路
在 Twitter 函数中定义好数据结构
followMap:用户关注列表, 用 Set 数据类型不需要去处理重复数据,取消关注(从列表删除)也会更方便;
postMap:用户推文列表;
latestPostId:推文的自增 id,用于后续获取推文列表时排序;
在 postTweet 函数中,将新增的 推文 { tweetId, postTime } 放到列表的最前面,并确保 latestPostId 自增;
在 follow 函数中,先检查 followMap 是否已存在 followerId 数据,若已存在,直接 add(followeeId), 若不存在,新增 new Set([followeeId]);
在 unfollow 函数中,直接检查是否存在 followMap[followerId] 列表,若存在直接 delete(followeeId);
在 getNewsFeed 函数中,因为要取用户和用户关注的用户的最新 10 条推文,所以只需要把这些用户的前 10 条推文取出来,再根据 postTime 去排序,然后取最新 10 条推文。
代码
/**
* Initialize your data structure here.
*/
var Twitter = function () {
this.followMap = {};
this.postMap = new Map();
this.latestPostId = 0;
};
/**
* Compose a new tweet.
* @param {number} userId
* @param {number} tweetId
* @return {void}
*/
Twitter.prototype.postTweet = function (userId, tweetId) {
const postTime = this.latestPostId++;
let tweeList = [{ tweetId, postTime }];
if (this.postMap.has(userId)) {
tweeList = tweeList.concat(this.postMap.get(userId));
}
this.postMap.set(userId, tweeList);
};
/**
* 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.
* @param {number} userId
* @return {number[]}
*/
Twitter.prototype.getNewsFeed = function (userId) {
const followeeIdList = this.followMap[userId]
? [...this.followMap[userId]]
: [];
const tweeList = [];
const userIds = [...new Set(followeeIdList.concat([userId]))];
userIds.forEach((uid) => {
if (this.postMap.has(uid)) {
tweeList.push(...this.postMap.get(uid).slice(0, 10));
}
});
tweeList.sort((a, b) => b.postTime - a.postTime);
return tweeList.slice(0, 10).map((item) => item.tweetId);
};
/**
* Follower follows a followee. If the operation is invalid, it should be a no-op.
* @param {number} followerId
* @param {number} followeeId
* @return {void}
*/
Twitter.prototype.follow = function (followerId, followeeId) {
if (this.followMap[followerId]) {
this.followMap[followerId].add(followeeId);
} else {
this.followMap[followerId] = new Set([followeeId]);
}
};
/**
* Follower unfollows a followee. If the operation is invalid, it should be a no-op.
* @param {number} followerId
* @param {number} followeeId
* @return {void}
*/
Twitter.prototype.unfollow = function (followerId, followeeId) {
if (this.followMap[followerId]) {
this.followMap[followerId].delete(followeeId);
}
};