package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/get-watched-videos-by-your-friends/
 */
public class Problem1311 {
    public List<String> watchedVideosByFriends(List<List<String>> watchedVideos,
                                               int[][] friends,
                                               int id,
                                               int level) {
        Map<String, Integer> countMap = new HashMap<>();
        watchedVideos(watchedVideos, friends, id, level, 0, new boolean[friends.length], countMap);
        List<Map.Entry<String, Integer>> sorted = new ArrayList<>(countMap.entrySet());
        Collections.sort(sorted, (a, b) -> {
            int cmp = Integer.compare(a.getValue(), b.getValue());
            if (cmp == 0) {
                return a.getKey().compareTo(b.getKey());
            }
            return cmp;
        });
        List<String> answer = new ArrayList<>();
        for (Map.Entry<String, Integer> e : sorted) {
            answer.add(e.getKey());
        }
        return answer;
    }

    private static void watchedVideos(List<List<String>> watchedVideos,
                                      int[][] friends, int id, int targetLevel,
                                      int level, boolean[] visited,
                                      Map<String, Integer> countMap) {
        if (visited[id]) {
            return;
        }
        visited[id] = true;
        if (level == targetLevel) {
            for (String video : watchedVideos.get(id)) {
                countMap.put(video, countMap.getOrDefault(video, 0) + 1);
            }
            return;
        }
        for (int friend : friends[id]) {
            watchedVideos(watchedVideos, friends, friend, targetLevel, level + 1,
                visited, countMap);
        }
    }

    public static void main(String[] args) {
        Problem1311 prob = new Problem1311();
        System.out.println(prob.watchedVideosByFriends(
            Arrays.asList(
                Arrays.asList("A", "B"), Arrays.asList("C"), Arrays.asList("B", "C"), Arrays.asList("D")
            ), new int[][]{{1, 2}, {0, 3}, {0, 3}, {1, 2}}, 0, 1)); // ["B","C"]
        System.out.println(prob.watchedVideosByFriends(
            Arrays.asList(
                Arrays.asList("A", "B"), Arrays.asList("C"), Arrays.asList("B", "C"), Arrays.asList("D")
            ), new int[][]{{1, 2, 3}, {0, 3}, {0, 3}, {0, 1, 2}}, 0, 1)); // ["B","D","C"]
        System.out.println(prob.watchedVideosByFriends(
            Arrays.asList(
                Arrays.asList("A", "B"), Arrays.asList("A", "B", "C"), Arrays.asList("B", "C"), Arrays.asList("D")
            ), new int[][]{{1, 2}, {0, 3}, {0, 3}, {1, 2}}, 0, 1)); // ["A","B","C"]
        System.out.println(prob.watchedVideosByFriends(
            Arrays.asList(
                Arrays.asList("A", "B"), Arrays.asList("C"), Arrays.asList("B", "C"), Arrays.asList("D")
            ), new int[][]{{1, 2}, {0, 3}, {0, 3}, {1, 2}}, 0, 2)); // ["D"]
    }
}

@@ -644,19 +644,16 @@
 , level,
- 0,
  new boo
@@ -676,32 +676,751 @@
 gth%5D, countMap);
+%0A%0A//        Queue%3CInteger%3E queue = new LinkedList%3C%3E();%0A//        queue.add(id);%0A//        int count = 0;%0A//        boolean%5B%5D visited = new boolean%5Bfriends.length%5D;%0A//        visited%5Bid%5D = true;%0A//        while (!queue.isEmpty()) %7B%0A//            if (count == level) %7B%0A//                break;%0A//            %7D%0A//            int size = queue.size();%0A//            for (int i = 0; i %3C size; i++) %7B%0A//                int p = queue.poll();%0A//                for (int friend : friends%5Bp%5D) %7B%0A//                    if (!visited%5Bfriend%5D) %7B%0A//                        queue.add(friend);%0A//                        visited%5Bfriend%5D = true;%0A//                    %7D%0A//                %7D%0A//            %7D%0A//            count++;%0A//        %7D
 %0A        List%3CMa
@@ -2057,23 +2057,17 @@
 id, int 
-targetL
+l
 evel,%0A  
@@ -2101,27 +2101,16 @@
         
- int level,
  boolean
@@ -2298,27 +2298,17 @@
 evel == 
-targetLevel
+0
 ) %7B%0A    
@@ -2511,32 +2511,107 @@
  friends%5Bid%5D) %7B%0A
+            if (visited%5Bfriend%5D) %7B%0A                continue;%0A            %7D%0A
             watc
@@ -2655,48 +2655,19 @@
 end,
- targetLevel,
  level 
-+
+-
  1,
-%0A               
  vis

Update problem 1311