@@ -70,67 +70,67 @@ class Employee {
70
70
// Needs to have a way of skipping employees already been searched
71
71
72
72
73
- // // RECURSION - Depth-First Search
74
- // // Convert List into HashMap with id as the key
75
- // // Store total importance value
76
- // // Get root employee
77
- // // if root employee has no subordinates
78
- // // return importance value
79
- // // else, loop over subordinates and recurse down
80
- // class Solution {
81
- // public Map<Integer, Employee> roster;
82
- //
83
- // public int getImportance(List<Employee> employees, int id) {
84
- //
85
- // if (employees == null) return 0;
86
- // roster = new HashMap<Integer, Employee>();
87
- //
88
- // for (Employee e : employees) {
89
- // roster.put(e.id, e);
90
- // }
91
- //
92
- // return calculateImportance(id);
93
- // }
94
- //
95
- // public int calculateImportance(int id) {
96
- // Employee lead = roster.get(id);
97
- // int totalImp = lead.importance;
98
- //
99
- // if (lead.subordinates.isEmpty()) {
100
- // return totalImp;
101
- // } else {
102
- // for (int subId : lead.subordinates) {
103
- // totalImp += calculateImportance(subId);
104
- // }
105
- // }
106
- //
107
- // return totalImp;
108
- // }
109
- // }
110
-
111
- // NESTED LOOPS
73
+ // RECURSION - Depth-First Search
74
+ // Convert List into HashMap with id as the key
75
+ // Store total importance value
76
+ // Get root employee
77
+ // if root employee has no subordinates
78
+ // return importance value
79
+ // else, loop over subordinates and recurse down
112
80
class Solution {
113
- HashMap <Integer , Employee > roster ;
81
+ public Map <Integer , Employee > roster ;
82
+
114
83
public int getImportance (List <Employee > employees , int id ) {
84
+
115
85
if (employees == null ) return 0 ;
116
- roster = new HashMap <>();
86
+ roster = new HashMap <Integer , Employee >();
117
87
118
88
for (Employee e : employees ) {
119
89
roster .put (e .id , e );
120
90
}
121
91
122
- int totalImp = 0 ;
123
- Queue <Employee > q = new LinkedList <>();
124
- q .add (roster .get (id ));
92
+ return calculateImportance (id );
93
+ }
125
94
126
- while (!q .isEmpty ()) {
127
- Employee curr = q .poll ();
128
- totalImp += curr .importance ;
129
- for (int subID : curr .subordinates ) {
130
- q .add (roster .get (subID ));
95
+ public int calculateImportance (int id ) {
96
+ Employee lead = roster .get (id );
97
+ int totalImp = lead .importance ;
98
+
99
+ if (lead .subordinates .isEmpty ()) {
100
+ return totalImp ;
101
+ } else {
102
+ for (int subId : lead .subordinates ) {
103
+ totalImp += calculateImportance (subId );
131
104
}
132
105
}
133
106
134
107
return totalImp ;
135
108
}
136
109
}
110
+
111
+ // // NESTED LOOPS
112
+ // class Solution {
113
+ // HashMap<Integer, Employee> roster;
114
+ // public int getImportance(List<Employee> employees, int id) {
115
+ // if (employees == null) return 0;
116
+ // roster = new HashMap<>();
117
+ //
118
+ // for (Employee e : employees) {
119
+ // roster.put(e.id, e);
120
+ // }
121
+ //
122
+ // int totalImp = 0;
123
+ // Queue<Employee> q = new LinkedList<>();
124
+ // q.add(roster.get(id));
125
+ //
126
+ // while (!q.isEmpty()) {
127
+ // Employee curr = q.poll();
128
+ // totalImp += curr.importance;
129
+ // for (int subID : curr.subordinates) {
130
+ // q.add(roster.get(subID));
131
+ // }
132
+ // }
133
+ //
134
+ // return totalImp;
135
+ // }
136
+ // }
0 commit comments