题目链接:

题目:
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods:

postTweet(userId, tweetId): Compose a new tweet.
getNewsFeed(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.
follow(followerId, followeeId): Follower follows a followee.
unfollow(followerId, followeeId): Follower unfollows a followee.
Example:

Twitter twitter = new Twitter();

// User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5);

// User 1’s news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1);

// User 1 follows user 2.
twitter.follow(1, 2);

// User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6);

// User 1’s news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1);

// User 1 unfollows user 2.
twitter.unfollow(1, 2);

// User 1’s news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);

思路:
用户之间的follow关系因为userId是唯一的,所以可以用HashMap保存user之间的follow关系。

如何设计用户的feed流呢?看到本题的tag有heap,想了一下没想到如何用heap实现feed, 那就暴力点,将所有用户发布的feed 用一个list保存。 当要获取某用户的feeds时,按时间顺序从list后往前,如果一个feed的userid属于该用户或其follower则 将该feed存入结果集。

题目不难,甚至有点简单。。follow和unfollow时间复杂度为O(1),空间复杂度为O(n),getNewsFeed时间复杂度为O(n),空间复杂度为O(1)。

算法:

HashMap<Integer, Set<Integer>> maps = new HashMap<Integer, Set<Integer>>();
    List<Feed> feeds = new ArrayList<Feed>();

    class Feed {
        int userId, tweetId;

        public Feed(int userId, int tweetId) {
            this.userId = userId;
            this.tweetId = tweetId;
        }

        public String toString(){
            return tweetId+"";
        }
    }

    /** Initialize your data structure here. */
    public Twitter() {

    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        Feed f = new Feed(userId, tweetId);
        feeds.add(f);
    }

    /**
     * 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.
     */
    public List<Integer> getNewsFeed(int userId) {
        List<Integer> res = new ArrayList<Integer>();
        Set<Integer> users = maps.get(userId);
        if(users==null)
            users=  new HashSet<Integer>();
        users.add(userId);
        for (int i=feeds.size()-1;i>=0;i--) {
            Feed f = feeds.get(i);
            if (res.size()<10&&users.contains(f.userId)) {
                res.add(f.tweetId);
            }
            if(res.size()>=10)
                break;
        }
        return res;
    }

    /**
     * Follower follows a followee. If the operation is invalid, it should be a
     * no-op.
     */
    public void follow(int followerId, int followeeId) {
        Set<Integer> sets;
        if (maps.containsKey(followerId)) {
            sets = maps.get(followerId);
        } else {
            sets = new HashSet<Integer>();
        }
        sets.add(followeeId);
        maps.put(followerId, sets);
    }

    /**
     * Follower unfollows a followee. If the operation is invalid, it should be
     * a no-op.
     */
    public void unfollow(int followerId, int followeeId) {
        Set<Integer> sets = maps.get(followerId);
        if(sets!=null&&sets.contains(followeeId)){
            sets.remove(followeeId);
            maps.put(followerId, sets);
        }
    }