design-twitter.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:lc-all-solutions 作者: csujedihy 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号