Skip to content

Commit b0fab84

Browse files
committed
update 690.employee-importance.java
1 parent 0609f03 commit b0fab84

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

690.employee-importance.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* @lc app=leetcode id=690 lang=java
3+
*
4+
* [690] Employee Importance
5+
*
6+
* https://leetcode.com/problems/employee-importance/description/
7+
*
8+
* algorithms
9+
* Easy (58.49%)
10+
* Total Accepted: 98.8K
11+
* Total Submissions: 168.9K
12+
* Testcase Example: '[[1,2,[2]], [2,3,[]]]\n2'
13+
*
14+
* You are given a data structure of employee information, which includes the
15+
* employee's unique id, their importance value and their direct subordinates'
16+
* id.
17+
*
18+
* For example, employee 1 is the leader of employee 2, and employee 2 is the
19+
* leader of employee 3. They have importance value 15, 10 and 5, respectively.
20+
* Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has
21+
* [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3
22+
* is also a subordinate of employee 1, the relationship is not direct.
23+
*
24+
* Now given the employee information of a company, and an employee id, you
25+
* need to return the total importance value of this employee and all their
26+
* subordinates.
27+
*
28+
* Example 1:
29+
*
30+
*
31+
* Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
32+
* Output: 11
33+
* Explanation:
34+
* Employee 1 has importance value 5, and he has two direct subordinates:
35+
* employee 2 and employee 3. They both have importance value 3. So the total
36+
* importance value of employee 1 is 5 + 3 + 3 = 11.
37+
*
38+
*
39+
*
40+
*
41+
* Note:
42+
*
43+
*
44+
* One employee has at most one direct leader and may have several
45+
* subordinates.
46+
* The maximum number of employees won't exceed 2000.
47+
*
48+
*
49+
*/
50+
/*
51+
// Definition for Employee.
52+
class Employee {
53+
public int id;
54+
public int importance;
55+
public List<Integer> subordinates;
56+
};
57+
*/
58+
59+
class Solution {
60+
public HashMap<Integer, Employee> roster = new HashMap<Integer, Employee>();
61+
62+
public int getImportance(List<Employee> employees, int id) {
63+
// Given information:
64+
// List if Employee class unique id, importance, List of subordinates' id
65+
// Max Employee 2000
66+
// At least 1 employee has direct leader and more than 1 subordinates
67+
// Return all importance value of current employee and the subordinates
68+
//
69+
// Recursion?
70+
// Nested loops?
71+
72+
// Convert List into HashMap with id as the key
73+
// Store total importance value
74+
// Get root employee
75+
// if root employee has no subordinates
76+
// return importance value
77+
// else, loop over subordinates and recurse down
78+
79+
for (Employee e : employees) {
80+
this.roster.put(e.id, e);
81+
}
82+
83+
return calculateImportance(id);
84+
}
85+
86+
public int calculateImportance(int id) {
87+
Employee lead = this.roster.get(id);
88+
int totalImp = lead.importance;
89+
90+
if (lead.subordinates.size() == 0) {
91+
return totalImp;
92+
} else {
93+
for (int subId : lead.subordinates) {
94+
totalImp += calculateImportance(subId);
95+
}
96+
}
97+
98+
return totalImp;
99+
}
100+
}

0 commit comments

Comments
 (0)