Inspired by "Making List of Tweets as Linked List" for easy merging, Beats 81%, Java


  • 0
    V
    public class Twitter 
    {
    	int counter;
    	HashMap<Integer, Tweet> userTweets;
    	HashMap<Integer, HashSet<Integer>> userAdjList;
    	class Tweet
    	{
    		int tweetId;
    		int timeStamp;
    		Tweet next;
    		public Tweet(int tId, int ts)
    		{
    			tweetId = tId;
    			timeStamp = ts;
    		}
    	}
    	/** Initialize your data structure here. */
    	public Twitter() 
    	{
    		counter = 0;
    		userTweets = new HashMap<Integer, Tweet>();
    		userAdjList = new HashMap<Integer, HashSet<Integer>>();
    	}
    	
    	/** Compose a new tweet. */
    	public void postTweet(int userId, int tweetId) 
    	{
    		Tweet t = new Tweet(tweetId, ++counter);
    		t.next = null;
    		if(!userTweets.containsKey(userId))
    			userTweets.put(userId, t);
    		else
    		{
    			t.next = userTweets.get(userId);
    			userTweets.put(userId, t);
    		}
    	}
    	
    	/** 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) 
    	{
    		PriorityQueue<Tweet> pq = new PriorityQueue<Tweet>(10, new Comparator<Tweet>()
    		{
    			@Override
    			public int compare(Tweet t1, Tweet t2)
    			{
    				return t2.timeStamp - t1.timeStamp;
    			}
    		});
    		HashSet<Integer> hs = userAdjList.get(userId);
    		if(hs == null)
    			hs = new HashSet<Integer>();
    		hs.add(userId);
    		for(int k : hs)
    			if(userTweets.containsKey(k))
    				pq.add(userTweets.get(k));
    		int i = 10;
    		List<Integer> toRet = new ArrayList<Integer>();
    		while(pq.size() > 0 && i > 0)
    		{
    			i--;
    			Tweet t = pq.poll();
    			toRet.add(t.tweetId);
    			if(t.next != null)
    				pq.add(t.next);
    		}
    		return toRet;
    	}
    	
    	/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    	public void follow(int followerId, int followeeId) 
    	{
    		HashSet<Integer> m;
    		if(!userAdjList.containsKey(followerId))
    		{
    			m = new HashSet<Integer>();
    			userAdjList.put(followerId, m);
    		}
    		else
    			m = userAdjList.get(followerId);
    		m.add(followeeId);
    	}
    	
    	/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    	public void unfollow(int followerId, int followeeId) 
    	{
    		if(userAdjList.containsKey(followerId))// && userAdjList.get(followerId).contains(followeeId))
    		{
    			Set<Integer> m;
    			m = userAdjList.get(followerId);
    			m.remove(followeeId);
    		}
    	}
    }
    
    /**
    * Your Twitter object will be instantiated and called as such:
    * Twitter obj = new Twitter();
    * obj.postTweet(userId,tweetId);
    * List<Integer> param_2 = obj.getNewsFeed(userId);
    * obj.follow(followerId,followeeId);
    * obj.unfollow(followerId,followeeId);
    */

Log in to reply
 

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