Get Watched Videos by Your Friends

There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.

Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest. 

Example 1:

Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"] 
Explanation: 
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"] 
Person with id = 2 -> watchedVideos = ["B","C"] 
The frequencies of watchedVideos by your friends are: 
B -> 1 
C -> 2

思路:题目意思比较难理解,清楚之后,大概是friends表示一个graph,然后给你一个startpoint,一个level,也就是BFS之后得到一系列friends,然后统计一下这些friends的movie,按照频率从小到大输出,然后相同频率就按照字母顺序输出;

class Solution {
    private class Node {
        public String movie;
        public int fre;
        public Node (String movie, int fre) {
            this.movie = movie;
            this.fre = fre;
        }
    }
    
    public class NodeComparator implements Comparator<Node> {
        @Override
        public int compare(Node a, Node b) {
            if(a.fre != b.fre) {
                return a.fre - b.fre;
            } else {
                return a.movie.compareTo(b.movie);
            }
        }
    }
    
    public List<String> watchedVideosByFriends( List<List<String>> watchedVideos, 
                                                int[][] friends, 
                                                int id, 
                                                int level) {
        HashMap<Integer, HashSet<Integer>> graph = new HashMap<>();
        for(int i = 0; i < friends.length; i++) {
            int person = i;
            for(int j = 0; j  < friends[i].length; j++) {
                int friend = friends[i][j];
                graph.putIfAbsent(person, new HashSet<Integer>());
                graph.putIfAbsent(friend, new HashSet<Integer>());
                graph.get(person).add(friend);
                graph.get(friend).add(person);
            }
        }
        
        List<Integer> friendAtLevel = bfs(graph, id, level);
        HashMap<String, Integer> hashmap = new HashMap<>();
        for(Integer index: friendAtLevel) {
            List<String> list = watchedVideos.get(index);
            for(String movie: list) {
                if(hashmap.containsKey(movie)) {
                    hashmap.put(movie, hashmap.get(movie) + 1);
                } else {
                    hashmap.put(movie, 1);
                }
            }
        }
        
        List<Node> watchedmovies = new ArrayList<Node>();
        for(String movie: hashmap.keySet()) {
            watchedmovies.add(new Node(movie, hashmap.get(movie)));
        }
        Collections.sort(watchedmovies, new NodeComparator());
        
        List<String> result = new ArrayList<String>();
        for(int i = 0; i < watchedmovies.size(); i++) {
            result.add(watchedmovies.get(i).movie);
        }
        return result;
    }
    
    private List<Integer> bfs(HashMap<Integer, 
                              HashSet<Integer>> graph,
                             int start, int k) {
        List<Integer> result = new ArrayList<Integer>();
        Queue<Integer> queue = new LinkedList<Integer>();
        HashSet<Integer> visited = new HashSet<Integer>();
        queue.offer(start);
        visited.add(start);
        
        int step = 0;
        boolean find = false;
        while(!queue.isEmpty() && !find) {
            int size = queue.size();
            for(int i = 0; i < size; i++) {
                Integer head = queue.poll();
                if(step == k) {
                    result.add(head);
                    find = true;
                    continue;
                } 
                for(Integer neighbor: graph.get(head)) {
                    if(!visited.contains(neighbor)) {
                        visited.add(neighbor);
                        queue.offer(neighbor);
                    }
                }
            }
            step++;
        }
        return result;
    }
}
发布了710 篇原创文章 · 获赞 13 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/u013325815/article/details/105308679