-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignTwitter.py
More file actions
106 lines (88 loc) · 3.74 KB
/
designTwitter.py
File metadata and controls
106 lines (88 loc) · 3.74 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/design-twitter/
# Author: Miao Zhang
# Date: 2021-02-04
class Twitter:
def __init__(self):
"""
Initialize your data structure here.
"""
self.timer = itertools.count(step=-1)
self.tweets = collections.defaultdict(collections.deque)
self.followees = collections.defaultdict(set)
def postTweet(self, userId: int, tweetId: int) -> None:
"""
Compose a new tweet.
"""
self.tweets[userId].appendleft((next(self.timer), tweetId))
def getNewsFeed(self, userId: int) -> List[int]:
"""
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.
"""
tweets = heapq.merge(*(self.tweets[u] for u in self.followees[userId] | {userId}))
return [t for _, t in itertools.islice(tweets, 10)]
def follow(self, followerId: int, followeeId: int) -> None:
"""
Follower follows a followee. If the operation is invalid, it should be a no-op.
"""
self.followees[followerId].add(followeeId)
def unfollow(self, followerId: int, followeeId: int) -> None:
"""
Follower unfollows a followee. If the operation is invalid, it should be a no-op.
"""
self.followees[followerId].discard(followeeId)
# 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)
##########################################################################################
class Twitter:
def __init__(self):
"""
Initialize your data structure here.
"""
self.timer = 0
self.tweets = collections.defaultdict(list)
self.followees = collections.defaultdict(set)
self.max_tweets = 10
def postTweet(self, userId: int, tweetId: int) -> None:
"""
Compose a new tweet.
"""
self.timer += 1
if len(self.tweets[userId]) == self.max_tweets:
self.tweets[userId].pop(0)
self.tweets[userId].append((self.timer, tweetId))
def getNewsFeed(self, userId: int) -> List[int]:
"""
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.
"""
ts = [i for i in self.tweets[userId]]
for uid in self.followees[userId]:
ts.extend(self.tweets[uid])
ts.sort(key=lambda x: x[0], reverse=True)
res = []
for i in range(min(self.max_tweets, len(ts))):
res.append(ts[i][1])
return res
def follow(self, followerId: int, followeeId: int) -> None:
"""
Follower follows a followee. If the operation is invalid, it should be a no-op.
"""
if followerId == followeeId: return
self.followees[followerId].add(followeeId)
def unfollow(self, followerId: int, followeeId: int) -> None:
"""
Follower unfollows a followee. If the operation is invalid, it should be a no-op.
"""
if followerId == followeeId: return
self.followees[followerId].discard(followeeId)
# 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)