Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit efb3377

Browse files
update 690.employee-importance.java
1 parent 33ba1cf commit efb3377

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed

‎690.employee-importance.java‎

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -70,67 +70,67 @@ class Employee {
7070
// Needs to have a way of skipping employees already been searched
7171

7272

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
11280
class Solution {
113-
HashMap<Integer, Employee> roster;
81+
public Map<Integer, Employee> roster;
82+
11483
public int getImportance(List<Employee> employees, int id) {
84+
11585
if (employees == null) return 0;
116-
roster = new HashMap<>();
86+
roster = new HashMap<Integer, Employee>();
11787

11888
for (Employee e : employees) {
11989
roster.put(e.id, e);
12090
}
12191

122-
int totalImp = 0;
123-
Queue<Employee> q = new LinkedList<>();
124-
q.add(roster.get(id));
92+
return calculateImportance(id);
93+
}
12594

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);
131104
}
132105
}
133106

134107
return totalImp;
135108
}
136109
}
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /