-
Notifications
You must be signed in to change notification settings - Fork 0
/
design_twitter.py
68 lines (50 loc) · 2.28 KB
/
design_twitter.py
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
62
63
64
65
66
67
68
# https://leetcode.com/problems/design-twitter/description/
# git add . && git commit -m "completed design_twitter" && git push && exit
class Twitter:
def __init__(self):
self.follow_dict = {}
self.post_dict = {}
self.post_count = 0
def postTweet(self, userId: int, tweetId: int) -> None:
user_posts = self.post_dict.get(userId,[])
user_posts.append((tweetId,self.post_count))
self.post_count += 1
self.post_dict[userId] = user_posts
def getNewsFeed(self, userId: int) -> List[int]:
result = []
pointer_dict = {}
user_posts = self.post_dict.get(userId,[])
if len(user_posts) > 0:
pointer_dict[userId] = len(user_posts) - 1
for following_user_id in self.follow_dict.get(userId,set()):
user_posts = self.post_dict.get(following_user_id,[])
if len(user_posts) > 0:
pointer_dict[following_user_id] = len(user_posts) -1
while len(result) != 10 and len(pointer_dict) != 0:
post_to_add = None
post_to_add_id = None
for id in pointer_dict:
if post_to_add == None or post_to_add[1] < self.post_dict[id][pointer_dict[id]][1]:
post_to_add = self.post_dict[id][pointer_dict[id]]
post_to_add_id = id
pointer_dict[post_to_add_id] -= 1
if pointer_dict[post_to_add_id] < 0:
del pointer_dict[post_to_add_id]
if post_to_add != None:
result.append(post_to_add[0])
return result
def follow(self, followerId: int, followeeId: int) -> None:
following = self.follow_dict.get(followerId,set())
following.add(followeeId)
self.follow_dict[followerId] = following
def unfollow(self, followerId: int, followeeId: int) -> None:
following = self.follow_dict.get(followerId,set())
if followeeId in following:
following.remove(followeeId)
self.follow_dict[followerId] = following
# Your Twitter object will be instantiated and called as such:
# obj = Twitter()
# obj.postTweet(userId,tweetId)
# param_2 = obj.getNewsFeed(userId)
# obj.follow(followerId,followeeId)
# obj.unfollow(followerId,followeeId)