优先级队列作为最大优先级队列未按预期工作

时间:2020-09-06 08:56:07

标签: java

我正在研究一个leetcode问题,我们需要设计基本的twitter功能, 例如关注和取消关注,发布一条推文和新闻提要,将为用户提供最新的10条推文

postTweet(userId,tweetId):编写一个新的tweet。 getNewsFeed(userId):检索用户新闻提要中的10个最新的tweet ID。新闻订阅源中的每个项目都必须由用户关注的用户或用户本人发布。必须按从最新到最近的顺序排列推文。 follow(followerId,followeeId):关注者关注关注者。 unfollow(followerId,followeeId):追随者取消关注。

import java.time.Instant;
import java.util.*;


class Twitter {

    Map<Integer, List<Integer>> followMap = new HashMap<Integer,List<Integer>>();
    Queue<Tweet> pQueue = new PriorityQueue<Tweet>((a, b)-> (int)(b.createdOn-a.createdOn));
    /** Initialize your data structure here. */
    public Twitter() {

    }

    /** Compose a new tweet. */

    public void postTweet(int userId, int tweetId) {
        Tweet tweet = new Tweet(userId,tweetId, System.nanoTime());
        pQueue.add(tweet);
    }

    /** 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> newsFeed = new ArrayList<Integer>();
        List<Integer> follwedList = followMap.get(userId);
        List<Tweet> la = new ArrayList<Tweet>();
        while(pQueue.peek()!=null){

            Tweet element = pQueue.poll();
            if(newsFeed.size()==10){
                pQueue.addAll(la);
                return newsFeed;
            }
            if(element.userId == userId || (follwedList!=null && follwedList.contains(element.userId))){
                newsFeed.add(element.tweetId);
            }
            la.add(element);
        }
        pQueue.addAll(la);
        return newsFeed;
    }

    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */

    public void follow(int followerId, int followeeId) {
        if(followMap.get(followerId)==null){
            List<Integer> a = new ArrayList<Integer>();
            a.add(followeeId);
            followMap.put(followerId,a);
        }else{
            followMap.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(followMap.get(followerId)!=null){
            for(int i=0;i<followMap.get(followerId).size();i++){
                if(followMap.get(followerId).get(i)==followeeId){
                    followMap.get(followerId).remove(i);
                    break;
                }
            }
        }
    }

}

class Tweet {
    int tweetId;
    int userId;
    long createdOn;
    Tweet(int userId, int tweetId, long createdOn){
        this.tweetId = tweetId;
        this.userId = userId;
        this.createdOn = createdOn;
    }
}

除一个测试用例外,大多数代码均按预期工作

[“ Twitter”,“ postTweet”,“ postTweet”,“ postTweet”,“ postTweet”,“ postTweet”,“ postTweet”,“ postTweet”,“ postTweet”,“ postTweet”,“ postTweet”,“ postTweet” “,” postTweet“,” postTweet“,” postTweet“,” postTweet“,” postTweet“,” postTweet“,” postTweet“,” postTweet“,” postTweet“,” postTweet“,” postTweet“,” getNewsFeed“, “关注”,“ getNewsFeed”,“取消关注”,“ getNewsFeed”] [[],[1,5],[2,3],[1,101],[2,13],[2,10],[1,2],[1,94],[2,505],[1,333 ],[2,22],[1,11],[1,205],[2,203],[1,201],[2,213],[1200],[2,202],[1,204],[2,208],[2,233], [1,222],[2,211],1,[1,2],1,[1,2],1]

我的答案

[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, [222,204,200,201,205,11,333,94,2,101],为空,[211,222,233,208,204,202,200,213,201,203],为空,[222,204,200,201,11,333,94,2,101,5]]

期望的答案

[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, [222,204,200,201,205,11,333,94,2,101],为空,[211,222,233,208,204,202,200,213,201,203],为空,[222,204,200,201,205,11,333,94,2,101]]

在最后一个新闻提要中有一个错误,对我来说205没来,但根据设计,我希望它会在结果中,不确定为什么会发生

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的策略:

            Tweet element = pQueue.poll();
            if(newsFeed.size()==10){
                pQueue.addAll(la);
                return newsFeed;
            }
            if(element.userId == userId || (follwedList!=null && follwedList.contains(element.userId))){
                newsFeed.add(element.tweetId);
            }
            la.add(element);

您在pQueue中拥有所有tweet,并且您的la列表会跟踪您(临时)已删除的tweet,一旦循环完成,您就将所有这些tweets添加到{{1} }返回pQueue。

如果是这样,那么在到达newsFeed.size()== 10的情况下,la不会包含所有已删除的推文。因为您早la

要解决此问题,您可能应该将return放在您的la.add(element)

相关问题