Easy understand Python getNewsFeed with simple data structure using heapq and hash table and hash set


  • 0
    M
    from heapq import *
    class Twitter(object):
    
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.timer = 0
            self.tweetDB = {}
            self.followDB = {}
    
        def postTweet(self, userId, tweetId):
            """
            Compose a new tweet.
            :type userId: int
            :type tweetId: int
            :rtype: void
            """
            self.timer += 1
            if userId in self.tweetDB:
                self.tweetDB[userId].append((-self.timer, tweetId))
            else:
                self.tweetDB[userId] = [(-self.timer, tweetId)]
            
    
        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]
            """
            tweets = []
            tweets += self.tweetDB.get(userId, [])
    
            if userId in self.followDB:
                for user in self.followDB.get(userId, set()):
                    tmp = self.tweetDB.get(user, [])
                    tweets += tmp       
    
            heapify(tweets)
            res = []
            while len(res) < 10 and tweets:
                res.append(heappop(tweets)[1])
    
            return res
    
        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:
                return
            if followerId not in self.followDB:
                self.followDB[followerId] = set([followeeId])
            else:
                self.followDB[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.followDB:
                # do nothing if there is no such followeeId
                self.followDB[followerId].discard(followeeId)
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.