Swift solution - Hash Table, Heap, Set


  • 0
    class Tweet {
        
        let id: Int
        let time: TimeInterval
        var next: Tweet?
        
        
        init(_ id: Int) {
            self.id = id
            time = NSDate().timeIntervalSince1970
            next = nil
        }
        
        
    }
    
    class User {
        
        let id: Int
        var followed: Set<Int>
        var tweetHead: Tweet?
        
        
        init(_ id: Int) {
            self.id = id
            followed = Set<Int>()
            follow(id)
            tweetHead = nil
        }
        
        func follow(_ id: Int) {
            followed.insert(id)
        }
        
        func unfollow(_ id: Int) {
            followed.remove(id)
        }
        
        func post(_ id: Int) {
            let tweet = Tweet(id)
            tweet.next = tweetHead
            tweetHead = tweet
        }
        
        
    }
    
    class Twitter {
        
        var userMap = [Int: User]()
        
        
        init() {
            userMap = [Int: User]()
        }
        
        func postTweet(_ userID: Int, _ tweetID: Int) {
            if !userMap.keys.contains(userID) {
                let user = User(userID)
                userMap[userID] = user
            }
            userMap[userID]?.post(tweetID)
        }
        
        func getNewsFeed(_ userID: Int) -> [Int] {
            guard let user = userMap[userID] else {
                return []
            }
            
            let users = user.followed
            var n = 10
            var result = [Int]()
            var maxHeap = Heap { (a: Tweet, b: Tweet) -> Bool in
                a.time > b.time
            }
            
            for user in users {
                let tweet = userMap[user]?.tweetHead
                if let tweet = tweet {
                    maxHeap.insert(tweet)
                }
            }
            
            while !maxHeap.isEmpty && n > 0 {
                let tweet = maxHeap.remove()
                result.append(tweet!.id)
                n -= 1
                if let nextTweet = tweet!.next {
                    maxHeap.insert(nextTweet)
                }
            }
            
            return result
        }
        
        func follow(_ followerID: Int, _ followeeID: Int) {
            if !userMap.keys.contains(followerID) {
                let user = User(followerID)
                userMap[followerID] = user
            }
            if !userMap.keys.contains(followeeID) {
                let user = User(followeeID)
                userMap[followeeID] = user
            }
            userMap[followerID]?.follow(followeeID)
        }
        
        func unfollow(_ followerID: Int, _ followeeID: Int) {
            if !userMap.keys.contains(followerID) {
                return
            }
            if !userMap.keys.contains(followeeID) {
                return
            }
            userMap[followerID]?.unfollow(followeeID)
        }
        
        
    }
    

Log in to reply
 

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