package leetcode;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * https://leetcode.com/problems/employee-importance/
 */
public class Problem690 {
    private static class Employee {
        // It's the unique id of each node;
        // unique id of this employee
        private int id;
        // the importance value of this employee
        private int importance;
        // the id of direct subordinates
        private List<Integer> subordinates;

        public Employee(int id, int importance, List<Integer> subordinates) {
            this.id = id;
            this.importance = importance;
            this.subordinates = subordinates;
        }
    };

    public int getImportance(List<Employee> employees, int id) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem690 prob = new Problem690();
        System.out.println(prob.getImportance(Arrays.asList(
            new Employee(1, 5, Arrays.asList(2, 3)),
            new Employee(2, 3, Collections.emptyList()),
            new Employee(3, 3, Collections.emptyList())),
            1)); // 11
    }
}

@@ -34,17 +34,20 @@
 il.Array
-s
+List
 ;%0Aimport
@@ -61,41 +61,107 @@
 til.
-Collections;%0Aimport java.util.Lis
+HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Se
 t;%0A%0A
@@ -420,12 +420,17 @@
 nce 
-valu
+importanc
 e of
@@ -769,17 +769,16 @@
  %7D%0A    %7D
-;
 %0A%0A    pu
@@ -848,381 +848,1544 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem690 prob = new Problem690();%0A        System.out.println(prob.getImportance(Arrays.asList(%0A            new Employee(1, 5, Arrays.asList(2, 3)),%0A            new Employee(2, 3, Collections.emptyList()),%0A            new Employee(3, 3, Collections.emptyList())),%0A            1)); // 11
+Map%3CInteger, List%3CNode%3E%3E adjList = new HashMap%3C%3E();%0A        Map%3CInteger, Node%3E nodeMap = new HashMap%3C%3E();%0A        for (Employee employee : employees) %7B%0A            if (!nodeMap.containsKey(employee.id)) %7B%0A                nodeMap.put(employee.id, new Node(employee.id, employee.importance));%0A            %7D%0A        %7D%0A        for (Employee employee : employees) %7B%0A            if (!adjList.containsKey(employee.id)) %7B%0A                adjList.put(employee.id, new ArrayList%3C%3E());%0A            %7D%0A            for (int subordinate : employee.subordinates) %7B%0A                adjList.get(employee.id).add(nodeMap.get(subordinate));%0A            %7D%0A        %7D%0A        return getImportance(adjList, id, nodeMap, new HashSet%3C%3E());%0A    %7D%0A%0A    private static int getImportance(Map%3CInteger, List%3CNode%3E%3E adjList, int id,%0A                                     Map%3CInteger, Node%3E nodeMap, Set%3CInteger%3E visited) %7B%0A        if (visited.contains(id)) %7B%0A            return 0;%0A        %7D%0A        visited.add(id);%0A        Node root = nodeMap.get(id);%0A        List%3CNode%3E children = adjList.get(root.id);%0A        int total = root.importance;%0A        if (children != null) %7B%0A            for (Node child : children) %7B%0A                total += getImportance(adjList, child.id, nodeMap, visited);%0A            %7D%0A        %7D%0A        return total;%0A    %7D%0A%0A    private static class Node %7B%0A        private final int id;%0A        private final int importance;%0A%0A        public Node(int id, int importance) %7B%0A            this.id = id;%0A            this.importance = importance;%0A        %7D
 %0A   

Solve problem 690