package leetcode;

/**
 * https://leetcode.com/problems/loud-and-rich/
 */
public class Problem851 {
    public int[] loudAndRich(int[][] richer, int[] quiet) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem851 prob = new Problem851();
        System.out.println(prob.loudAndRich(new int[][]{
            {1, 0}, {2, 1}, {3, 1}, {3, 7}, {4, 3}, {5, 3}, {6, 3}
        }, new int[]{3, 2, 5, 4, 6, 1, 7, 0})); // [5,5,2,5,4,5,6,7]
    }
}

@@ -12,16 +12,116 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -266,325 +266,1685 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem851 prob = new Problem851();%0A        System.out.println(prob.loudAndRich(new int%5B%5D%5B%5D%7B%0A            %7B1, 0%7D, %7B2, 1%7D, %7B3, 1%7D, %7B3, 7%7D, %7B4, 3%7D, %7B5, 3%7D, %7B6, 3%7D%0A        %7D, new int%5B%5D%7B3, 2, 5, 4, 6, 1, 7, 0%7D)); // %5B5,5,2,5,4,5,6,7%5D
+Map%3CInteger, List%3CInteger%3E%3E graph = buildGraph(richer);%0A        int%5B%5D answers = new int%5Bquiet.length%5D;%0A        for (int i = 0; i %3C quiet.length; i++) %7B%0A            boolean%5B%5D visited = new boolean%5Bquiet.length%5D;%0A            Quietest quietest = new Quietest();%0A            dfs(graph, i, visited, quiet, quietest);%0A            answers%5Bi%5D = quietest.index;%0A        %7D%0A        return answers;%0A    %7D%0A%0A    private static class Quietest %7B%0A        private int value = Integer.MAX_VALUE;%0A        private int index = -1;%0A    %7D%0A%0A    private static void dfs(Map%3CInteger, List%3CInteger%3E%3E graph,%0A                            int source,%0A                            boolean%5B%5D visited,%0A                            int%5B%5D quiet,%0A                            Quietest quietest) %7B%0A        if (quietest.value %3E= quiet%5Bsource%5D) %7B%0A            quietest.value = quiet%5Bsource%5D;%0A            quietest.index = source;%0A        %7D%0A        visited%5Bsource%5D = true;%0A        List%3CInteger%3E adjacents = graph.get(source);%0A        if (adjacents == null) %7B%0A            return;%0A        %7D%0A        for (int adj : adjacents) %7B%0A            if (!visited%5Badj%5D) %7B%0A                dfs(graph, adj, visited, quiet, quietest);%0A            %7D%0A        %7D%0A    %7D%0A    private static Map%3CInteger, List%3CInteger%3E%3E buildGraph(int%5B%5D%5B%5D richer) %7B%0A        Map%3CInteger, List%3CInteger%3E%3E graph = new HashMap%3C%3E();%0A        for (int%5B%5D rich : richer) %7B%0A            if (!graph.containsKey(rich%5B1%5D)) %7B%0A                List%3CInteger%3E newList = new ArrayList%3C%3E();%0A                newList.add(rich%5B0%5D);%0A                graph.put(rich%5B1%5D, newList);%0A            %7D else %7B%0A                graph.get(rich%5B1%5D).add(rich%5B0%5D);%0A            %7D%0A        %7D%0A        return graph;
 %0A   

Solve problem 851