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 2d8fb24

Browse files
Add Solution3.java to problems 0690
1 parent a64ff97 commit 2d8fb24

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
// Employee info
3+
class Employee {
4+
// It's the unique id of each node;
5+
// unique id of this employee
6+
public int id;
7+
// the importance value of this employee
8+
public int importance;
9+
// the id of direct subordinates
10+
public List<Integer> subordinates;
11+
};
12+
*/
13+
14+
import java.util.*;
15+
16+
class Solution {
17+
public int getImportance(List<Employee> employees, int id) {
18+
Map<Integer, Employee> map = new HashMap<>();
19+
for (Employee employee : employees) {
20+
map.put(employee.id, employee);
21+
}
22+
Stack<Employee> stack = new Stack<>();
23+
stack.add(map.get(id));
24+
int ant = 0;
25+
while (!stack.isEmpty()) {
26+
Employee pop = stack.pop();
27+
ant += pop.importance;
28+
for (Integer subordinate : pop.subordinates) {
29+
stack.add(map.get(subordinate));
30+
}
31+
}
32+
return ant;
33+
}
34+
}

0 commit comments

Comments
(0)

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