Skip to content

Commit 4e4782a

Browse files
authored
Added task 690.
1 parent 41908f1 commit 4e4782a

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0601_0700.s0690_employee_importance;
2+
3+
// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search
4+
5+
/*
6+
// Definition for Employee.
7+
class Employee {
8+
public int id;
9+
public int importance;
10+
public List<Integer> subordinates;
11+
};
12+
*/
13+
14+
import com_github_leetcode.Employee;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
public class Solution {
20+
public int getImportance(List<Employee> employees, int id) {
21+
Map<Integer, Employee> map = new HashMap<>();
22+
for (Employee emp : employees) {
23+
map.put(emp.id, emp);
24+
}
25+
return calculateImportance(id, map);
26+
}
27+
28+
private int calculateImportance(int id, Map<Integer, Employee> map) {
29+
Employee employee = map.get(id);
30+
int sum = employee.importance;
31+
for (int sub : employee.subordinates) {
32+
sum += calculateImportance(sub, map);
33+
}
34+
return sum;
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
690\. Employee Importance
2+
3+
Medium
4+
5+
You have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs.
6+
7+
You are given an array of employees `employees` where:
8+
9+
* `employees[i].id` is the ID of the <code>i<sup>th</sup></code> employee.
10+
* `employees[i].importance` is the importance value of the <code>i<sup>th</sup></code> employee.
11+
* `employees[i].subordinates` is a list of the IDs of the direct subordinates of the <code>i<sup>th</sup></code> employee.
12+
13+
Given an integer `id` that represents an employee's ID, return _the **total** importance value of this employee and all their direct and indirect subordinates_.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/05/31/emp1-tree.jpg)
18+
19+
**Input:** employees = [[1,5,[2,3]],[2,3,[]],[3,3,[]]], id = 1
20+
21+
**Output:** 11
22+
23+
**Explanation:**
24+
25+
Employee 1 has an importance value of 5 and has two direct subordinates: employee 2 and employee 3.
26+
27+
They both have an importance value of 3.
28+
29+
Thus, the total importance value of employee 1 is 5 + 3 + 3 = 11.
30+
31+
**Example 2:**
32+
33+
![](https://assets.leetcode.com/uploads/2021/05/31/emp2-tree.jpg)
34+
35+
**Input:** employees = [[1,2,[5]],[5,-3,[]]], id = 5
36+
37+
**Output:** -3
38+
39+
**Explanation:**
40+
41+
Employee 5 has an importance value of -3 and has no direct subordinates.
42+
43+
Thus, the total importance value of employee 5 is -3.
44+
45+
**Constraints:**
46+
47+
* `1 <= employees.length <= 2000`
48+
* `1 <= employees[i].id <= 2000`
49+
* All `employees[i].id` are **unique**.
50+
* `-100 <= employees[i].importance <= 100`
51+
* One employee has at most one direct leader and may have several subordinates.
52+
* The IDs in `employees[i].subordinates` are valid IDs.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0601_0700.s0690_employee_importance;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.Employee;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.jupiter.api.Test;
10+
11+
class SolutionTest {
12+
@Test
13+
void getImportance() {
14+
List<Employee> employees =
15+
Arrays.asList(
16+
new Employee(1, 5, Arrays.asList(2, 3)),
17+
new Employee(2, 3, Arrays.asList()),
18+
new Employee(3, 3, Arrays.asList()));
19+
assertThat(new Solution().getImportance(employees, 1), equalTo(11));
20+
}
21+
22+
@Test
23+
void getImportance2() {
24+
List<Employee> employees =
25+
Arrays.asList(
26+
new Employee(1, 5, Arrays.asList(2, 3)),
27+
new Employee(2, 3, Arrays.asList(4)),
28+
new Employee(3, 4, Arrays.asList()),
29+
new Employee(4, 1, Arrays.asList()));
30+
assertThat(new Solution().getImportance(employees, 1), equalTo(13));
31+
}
32+
}

0 commit comments

Comments
 (0)