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]
"""
ret = []
heap = []
if self.tweets[userId]:
heapq.heappush(heap, self.tweets[userId][-1])
for followeeId in self.friendship[userId]:
if self.tweets[followeeId]:
heapq.heappush(heap, self.tweets[followeeId][-1])
cnt = 10
while heap and cnt > 0:
_, tid, uid, idx = heapq.heappop(heap)
ret.append(tid)
if idx > 0:
heapq.heappush(heap, self.tweets[uid][idx - 1])
cnt -= 1
return ret
评论列表
文章目录