public class Tweet {
static int[] getRecommendedTweets(int[][] followGraph_edges, int[][] likeGraph_edges,
int targetUser, int minLikeThreshold) {
Set<Integer> targetUsersFollows = new HashSet<>();
for (int i[]: followGraph_edges){
if(i[0]==targetUser) targetUsersFollows.add(i[1]);
}
Map<Integer,Integer> recomTweets = new HashMap<>();
for(int i[]:likeGraph_edges){
if(targetUsersFollows.contains(i[0])){
if (recomTweets.containsKey(i[1])){
recomTweets.put(i[1],recomTweets.get(i[1])+1);
}
else recomTweets.put(i[1],1);
}
}
ArrayList<Integer>ansList = new ArrayList<>();
for(int tweet: recomTweets.keySet()){
if(recomTweets.get(tweet)>=minLikeThreshold){
ansList.add(tweet);
}
}
int[] ans = new int [ansList.size()];
for(int i=0;i<ansList.size();i++){
ans[i]=ansList.get(i);
}
Arrays.sort(ans);
return ans;
}
public static void main(String[] args) throws Exception {
int[][] followGraph_edges ={{1,2},{1,3},{1,4},{2,3},{2,4},{1,5},{1,6},{1,7},{2,3},{1,11},{1,20},{2,9}};
int[][] likeGraph_edges ={{2,17},{3,17},{4,17},{7,17},{6,17},{5,12},{3,12},{4,12},{11,12},{20,13},{9,13}};
int [] ans = getRecommendedTweets(followGraph_edges,likeGraph_edges,1,3);
for(int i:ans){
System.out.println(i);
}
}
}
就用hashset 和 hashmap 读就成