Skip to content

Commit 9e3c20f

Browse files
authored
feat: add solutions to lc problem: No.690 (doocs#2890)
No.0690.Employee Importance
1 parent 58c1c88 commit 9e3c20f

File tree

8 files changed

+336
-86
lines changed

8 files changed

+336
-86
lines changed

solution/0600-0699/0690.Employee Importance/README.md

Lines changed: 118 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ tags:
7777

7878
<!-- solution:start -->
7979

80-
### 方法一
80+
### 方法一:哈希表 + DFS
81+
82+
我们用一个哈希表 $d$ 存储所有员工的信息,其中键是员工的 ID,值是员工对象。然后我们从给定的员工 ID 开始深度优先搜索,每次遍历到一个员工时,将该员工的重要度加到答案中,并递归遍历该员工的所有下属,将下属的重要度也加到答案中。
83+
84+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是员工的数量。
8185

8286
<!-- tabs:start -->
8387

@@ -95,16 +99,11 @@ class Employee:
9599

96100

97101
class Solution:
98-
def getImportance(self, employees: List['Employee'], id: int) -> int:
99-
m = {emp.id: emp for emp in employees}
100-
101-
def dfs(id: int) -> int:
102-
emp = m[id]
103-
s = emp.importance
104-
for sub in emp.subordinates:
105-
s += dfs(sub)
106-
return s
102+
def getImportance(self, employees: List["Employee"], id: int) -> int:
103+
def dfs(i: int) -> int:
104+
return d[i].importance + sum(dfs(j) for j in d[i].subordinates)
107105

106+
d = {e.id: e for e in employees}
108107
return dfs(id)
109108
```
110109

@@ -121,24 +120,115 @@ class Employee {
121120
*/
122121

123122
class Solution {
124-
125-
private final Map<Integer, Employee> map = new HashMap<>();
123+
private final Map<Integer, Employee> d = new HashMap<>();
126124

127125
public int getImportance(List<Employee> employees, int id) {
128-
for (Employee employee : employees) {
129-
map.put(employee.id, employee);
126+
for (var e : employees) {
127+
d.put(e.id, e);
130128
}
131129
return dfs(id);
132130
}
133131

134-
private int dfs(int id) {
135-
Employee employee = map.get(id);
136-
int sum = employee.importance;
137-
for (Integer subordinate : employee.subordinates) {
138-
sum += dfs(subordinate);
132+
private int dfs(int i) {
133+
Employee e = d.get(i);
134+
int s = e.importance;
135+
for (int j : e.subordinates) {
136+
s += dfs(j);
137+
}
138+
return s;
139+
}
140+
}
141+
```
142+
143+
#### C++
144+
145+
```cpp
146+
/*
147+
// Definition for Employee.
148+
class Employee {
149+
public:
150+
int id;
151+
int importance;
152+
vector<int> subordinates;
153+
};
154+
*/
155+
156+
class Solution {
157+
public:
158+
int getImportance(vector<Employee*> employees, int id) {
159+
unordered_map<int, Employee*> d;
160+
for (auto& e : employees) {
161+
d[e->id] = e;
139162
}
140-
return sum;
163+
function<int(int)> dfs = [&](int i) -> int {
164+
int s = d[i]->importance;
165+
for (int j : d[i]->subordinates) {
166+
s += dfs(j);
167+
}
168+
return s;
169+
};
170+
return dfs(id);
141171
}
172+
};
173+
```
174+
175+
```go
176+
/**
177+
* Definition for Employee.
178+
* type Employee struct {
179+
* Id int
180+
* Importance int
181+
* Subordinates []int
182+
* }
183+
*/
184+
185+
func getImportance(employees []*Employee, id int) int {
186+
d := map[int]*Employee{}
187+
for _, e := range employees {
188+
d[e.Id] = e
189+
}
190+
var dfs func(int) int
191+
dfs = func(i int) int {
192+
s := d[i].Importance
193+
for _, j := range d[i].Subordinates {
194+
s += dfs(j)
195+
}
196+
return s
197+
}
198+
return dfs(id)
199+
}
200+
```
201+
202+
#### TypeScript
203+
204+
```ts
205+
/**
206+
* Definition for Employee.
207+
* class Employee {
208+
* id: number
209+
* importance: number
210+
* subordinates: number[]
211+
* constructor(id: number, importance: number, subordinates: number[]) {
212+
* this.id = (id === undefined) ? 0 : id;
213+
* this.importance = (importance === undefined) ? 0 : importance;
214+
* this.subordinates = (subordinates === undefined) ? [] : subordinates;
215+
* }
216+
* }
217+
*/
218+
219+
function getImportance(employees: Employee[], id: number): number {
220+
const d = new Map<number, Employee>();
221+
for (const e of employees) {
222+
d.set(e.id, e);
223+
}
224+
const dfs = (i: number): number => {
225+
let s = d.get(i)!.importance;
226+
for (const j of d.get(i)!.subordinates) {
227+
s += dfs(j);
228+
}
229+
return s;
230+
};
231+
return dfs(id);
142232
}
143233
```
144234

@@ -160,17 +250,16 @@ class Solution {
160250
* @return {number}
161251
*/
162252
var GetImportance = function (employees, id) {
163-
const map = new Map();
164-
for (const employee of employees) {
165-
map.set(employee.id, employee);
253+
const d = new Map();
254+
for (const e of employees) {
255+
d.set(e.id, e);
166256
}
167-
const dfs = id => {
168-
const employee = map.get(id);
169-
let sum = employee.importance;
170-
for (const subId of employee.subordinates) {
171-
sum += dfs(subId);
257+
const dfs = i => {
258+
let s = d.get(i).importance;
259+
for (const j of d.get(i).subordinates) {
260+
s += dfs(j);
172261
}
173-
return sum;
262+
return s;
174263
};
175264
return dfs(id);
176265
};

solution/0600-0699/0690.Employee Importance/README_EN.md

Lines changed: 118 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ Thus, the total importance value of employee 5 is -3.
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Hash Table + DFS
74+
75+
We use a hash table $d$ to store all employee information, where the key is the employee's ID, and the value is the employee object. Then we start a depth-first search from the given employee ID. Each time we traverse to an employee, we add the employee's importance to the answer, and recursively traverse all the subordinates of the employee, adding the importance of the subordinates to the answer as well.
76+
77+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of employees.
7478

7579
<!-- tabs:start -->
7680

@@ -88,16 +92,11 @@ class Employee:
8892

8993

9094
class Solution:
91-
def getImportance(self, employees: List['Employee'], id: int) -> int:
92-
m = {emp.id: emp for emp in employees}
93-
94-
def dfs(id: int) -> int:
95-
emp = m[id]
96-
s = emp.importance
97-
for sub in emp.subordinates:
98-
s += dfs(sub)
99-
return s
95+
def getImportance(self, employees: List["Employee"], id: int) -> int:
96+
def dfs(i: int) -> int:
97+
return d[i].importance + sum(dfs(j) for j in d[i].subordinates)
10098

99+
d = {e.id: e for e in employees}
101100
return dfs(id)
102101
```
103102

@@ -114,24 +113,115 @@ class Employee {
114113
*/
115114

116115
class Solution {
117-
118-
private final Map<Integer, Employee> map = new HashMap<>();
116+
private final Map<Integer, Employee> d = new HashMap<>();
119117

120118
public int getImportance(List<Employee> employees, int id) {
121-
for (Employee employee : employees) {
122-
map.put(employee.id, employee);
119+
for (var e : employees) {
120+
d.put(e.id, e);
123121
}
124122
return dfs(id);
125123
}
126124

127-
private int dfs(int id) {
128-
Employee employee = map.get(id);
129-
int sum = employee.importance;
130-
for (Integer subordinate : employee.subordinates) {
131-
sum += dfs(subordinate);
125+
private int dfs(int i) {
126+
Employee e = d.get(i);
127+
int s = e.importance;
128+
for (int j : e.subordinates) {
129+
s += dfs(j);
130+
}
131+
return s;
132+
}
133+
}
134+
```
135+
136+
#### C++
137+
138+
```cpp
139+
/*
140+
// Definition for Employee.
141+
class Employee {
142+
public:
143+
int id;
144+
int importance;
145+
vector<int> subordinates;
146+
};
147+
*/
148+
149+
class Solution {
150+
public:
151+
int getImportance(vector<Employee*> employees, int id) {
152+
unordered_map<int, Employee*> d;
153+
for (auto& e : employees) {
154+
d[e->id] = e;
132155
}
133-
return sum;
156+
function<int(int)> dfs = [&](int i) -> int {
157+
int s = d[i]->importance;
158+
for (int j : d[i]->subordinates) {
159+
s += dfs(j);
160+
}
161+
return s;
162+
};
163+
return dfs(id);
134164
}
165+
};
166+
```
167+
168+
```go
169+
/**
170+
* Definition for Employee.
171+
* type Employee struct {
172+
* Id int
173+
* Importance int
174+
* Subordinates []int
175+
* }
176+
*/
177+
178+
func getImportance(employees []*Employee, id int) int {
179+
d := map[int]*Employee{}
180+
for _, e := range employees {
181+
d[e.Id] = e
182+
}
183+
var dfs func(int) int
184+
dfs = func(i int) int {
185+
s := d[i].Importance
186+
for _, j := range d[i].Subordinates {
187+
s += dfs(j)
188+
}
189+
return s
190+
}
191+
return dfs(id)
192+
}
193+
```
194+
195+
#### TypeScript
196+
197+
```ts
198+
/**
199+
* Definition for Employee.
200+
* class Employee {
201+
* id: number
202+
* importance: number
203+
* subordinates: number[]
204+
* constructor(id: number, importance: number, subordinates: number[]) {
205+
* this.id = (id === undefined) ? 0 : id;
206+
* this.importance = (importance === undefined) ? 0 : importance;
207+
* this.subordinates = (subordinates === undefined) ? [] : subordinates;
208+
* }
209+
* }
210+
*/
211+
212+
function getImportance(employees: Employee[], id: number): number {
213+
const d = new Map<number, Employee>();
214+
for (const e of employees) {
215+
d.set(e.id, e);
216+
}
217+
const dfs = (i: number): number => {
218+
let s = d.get(i)!.importance;
219+
for (const j of d.get(i)!.subordinates) {
220+
s += dfs(j);
221+
}
222+
return s;
223+
};
224+
return dfs(id);
135225
}
136226
```
137227

@@ -153,17 +243,16 @@ class Solution {
153243
* @return {number}
154244
*/
155245
var GetImportance = function (employees, id) {
156-
const map = new Map();
157-
for (const employee of employees) {
158-
map.set(employee.id, employee);
246+
const d = new Map();
247+
for (const e of employees) {
248+
d.set(e.id, e);
159249
}
160-
const dfs = id => {
161-
const employee = map.get(id);
162-
let sum = employee.importance;
163-
for (const subId of employee.subordinates) {
164-
sum += dfs(subId);
250+
const dfs = i => {
251+
let s = d.get(i).importance;
252+
for (const j of d.get(i).subordinates) {
253+
s += dfs(j);
165254
}
166-
return sum;
255+
return s;
167256
};
168257
return dfs(id);
169258
};

0 commit comments

Comments
 (0)