leetcode Design Twitter
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods:
- postTweet(userId, tweetId): Compose a new tweet.
- getNewsFeed(userId): 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.
- follow(followerId, followeeId): Follower follows a followee.
- unfollow(followerId, followeeId): Follower unfollows a followee.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 Twitter twitter = new Twitter();
// User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5);
// User 1's news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1);
// User 1 follows user 2.
twitter.follow(1, 2);
// User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6);
// User 1's news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1);
// User 1 unfollows user 2.
twitter.unfollow(1, 2);
// User 1's news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);
题意:要求设计一个数据结构,使其能满足twitter的4种基本操作,发推、获得关注用户和自身最新10条推文、关注用户和取消关注。
思路:水题。和上一题一样,不明白为啥为hard。 可能难点就在于,直接写出无bug 的code吧。
需要那些非法的情况需要进行考虑吧。比如:
- follow操作 followerId, followeeId 相等
- unfollow操作followerId 不存在,或者followerId 压根就没关注 followeeId
求10个最近的推文可以用堆(当然这里我没有)
Python 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62class Twitter(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.tweets_cnt = 0
self.tweets = collections.defaultdict(list)
self.follower_ship = collections.defaultdict(set)
def postTweet(self, userId, tweetId):
"""
Compose a new tweet.
:type userId: int
:type tweetId: int
:rtype: void
"""
self.tweets[userId].append([tweetId, self.tweets_cnt])
self.tweets_cnt += 1
def getNewsFeed(self, userId):
"""
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.
:type userId: int
:rtype: List[int]
"""
recent_tweets = []
user_list = list(self.follower_ship[userId]) + [userId]
userId_tweet_index = [[userId, len(self.tweets[userId]) - 1] for userId in user_list if userId in self.tweets]
for _ in xrange(10):
max_index = max_tweet_id = max_user_id = -1
for i, (user_id, tweet_index) in enumerate(userId_tweet_index):
if tweet_index >= 0:
tweet_info = self.tweets[user_id][tweet_index]
if tweet_info[1] > max_tweet_id:
max_index, max_tweet_id, max_user_id = i, tweet_info[1], user_id
if max_index < 0: break
recent_tweets.append(self.tweets[max_user_id][userId_tweet_index[max_index][1]][0])
userId_tweet_index[max_index][1] -= 1
return recent_tweets
def follow(self, followerId, followeeId):
"""
Follower follows a followee. If the operation is invalid, it should be a no-op.
:type followerId: int
:type followeeId: int
:rtype: void
"""
if followerId != followeeId:
self.follower_ship[followerId].add(followeeId)
def unfollow(self, followerId, followeeId):
"""
Follower unfollows a followee. If the operation is invalid, it should be a no-op.
:type followerId: int
:type followeeId: int
:rtype: void
"""
if followerId in self.follower_ship and followeeId in self.follower_ship[followerId]:
self.follower_ship[followerId].remove(followeeId)
本题是leetcode 355 Design Twitter 题解
更多题解可以查看:https://www.hrwhisper.me/leetcode-algorithm-solution/