Missing Test case


  • 0
    H

    My code will get Runtime Error when running following testcase

    ["Twitter","follow","getNewsFeed"]
    [[],[1,5],[1]]
    

    But my code is accepted by OJ, so please add this testcase.
    My code:

    HashMap<Integer, Set<Integer>> following;
    HashMap<Integer, List<Timestamp>> posts;
    int tweetCounter;
    //int userCounter;
    private class Timestamp{
        int id;
        int twitterId;
        Timestamp(int id, int twitterId){
            this.id = id;
            this.twitterId = twitterId;
        }
    }
    
    
    /** Initialize your data structure here. */
    public Twitter() {
        following = new HashMap<Integer, Set<Integer>>();
        posts = new HashMap<Integer, List<Timestamp>>();
    }
    
    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if(!posts.containsKey(userId)){
            posts.put( userId, new ArrayList<Timestamp>() );
        }
        posts.get(userId).add(new Timestamp(tweetCounter++, tweetId));
    }
    
    /** 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<Timestamp> que = new PriorityQueue<Timestamp>(new Comparator<Timestamp>(){
            public int compare(Timestamp a, Timestamp b){
                return b.id - a.id;   
            }
        }  );
        
        if(following.containsKey(userId)){
            for( Integer followeeId : following.get(userId)   ){
                for(Timestamp time :  posts.get(followeeId) ){
                    que.offer( time );
                }
            }            
        }
        
    
        
        if(posts.containsKey(userId)){
            for(Timestamp time :  posts.get(userId)){
                que.offer(time);
            }        	
        }
        
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < 10 && !que.isEmpty(); i++){
            list.add( que.poll().twitterId );
        }
        return list;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        if( followerId ==followeeId ) return;
        
        if(!following.containsKey(followerId)){
            following.put(followerId, new HashSet<Integer>() );
        }
        following.get(followerId).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( !following.containsKey(followerId) ) return;
        
        Set<Integer> list = following.get(followerId);
        
        list.remove(followeeId);
        
    }

  • 0

    Thanks, I have added your test case.


Log in to reply
 

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