活跃农民
- 积分
- 511
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-6-5
- 最后登录
- 1970-1-1
|
1209 10. Design Twitter
/**
* Initialize your data structure here.
*/
var Twitter = function() {
this.users = new Map();
};
let counter = 0;
class User {
constructor (id) {
this.id = id;
this.followees = new Set([id]);
this.tweets = [];
}
}
class Tweet {
constructor (id) {
this.id = id;
this.time = counter++; // !!! could be within the same second !!!
}
}
/**
* Compose a new tweet.
* @param {number} userId
* @param {number} tweetId
* @return {void}
*/
Twitter.prototype.postTweet = function(userId, tweetId) {
const user = this.users.get(userId) || new User(userId);
this.users.set(userId, user);
user.tweets.unshift(new Tweet(tweetId)); // !!! because later we start from index 0 to retrieve the latest
};
/**
* 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 user = this.users.get(userId);
if (user) {
const lists = [...user.followees].map(followeeId => this.users.get(followeeId).tweets);
const feeds = [];
const indices = Array(lists.length).fill(0);
for (let i = 0; i < 10; i++) {
// get maxTime
let maxTime = 0;
for (let j = 0; j < indices.length; j++) {
const list = lists[j];
const index = indices[j];
const tweet = list[index];
if (tweet && tweet.time > maxTime) {
maxTime = tweet.time;
}
}
// update index of maxTime
for (let j = 0; j < indices.length; j++) {
const list = lists[j];
const index = indices[j];
const tweet = list[index];
if (tweet && tweet.time === maxTime) {
feeds.push(tweet.id);
indices[j]++;
}
}
}
return feeds;
} else {
return [];
}
};
/**
* 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) {
const follower = this.users.get(followerId) || new User(followerId);
this.users.set(followerId, follower);
const followee = this.users.get(followeeId) || new User(followeeId);
this.users.set(followeeId, followee);
follower.followees.add(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 (followerId != followeeId && this.users.has(followerId) && this.users.has(followeeId)) { // !!!! cannot unfollow himself
const follower = this.users.get(followerId);
follower.followees.delete(followeeId);
}
};
/**
* Your Twitter object will be instantiated and called as such:
* var obj = Object.create(Twitter).createNew()
* obj.postTweet(userId,tweetId)
* var param_2 = obj.getNewsFeed(userId)
* obj.follow(followerId,followeeId)
* obj.unfollow(followerId,followeeId)
*/
const t = new Twitter();
t.postTweet(1, 5)
t.getNewsFeed(1)
有一个更好的办法,用linkedlist装tweets, 用heap 来找10个 |
|