diff --git a/EmployeeImportance.java b/EmployeeImportance.java new file mode 100644 index 0000000..a144129 --- /dev/null +++ b/EmployeeImportance.java @@ -0,0 +1,23 @@ +// Time Complexity : O(n) +// Spcae Complexity: O(n) +// Explanaytion : I first store every employee in a HashMap using their id so that I can retrieve any employee in O(1) time. Then I use a queue to perform a BFS starting from the given id, adding all subordinates level by level. For each employee I dequeue, I add their importance to the total and push their subordinates into the queue. +class Solution { + public int getImportance(List employees, int id) { + Map map = new HashMap<>(); + Queue q = new LinkedList<>(); + for(int i=0; i q = new LinkedList<>(); + for(int i =0; i=0 && j>=0 && i