Skip to content

Commit 7327817

Browse files
committed
690_Employee_Importance
1 parent 16c38cc commit 7327817

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
615615
|48| [1376. Time Needed to Inform All Employees](https://tinyurl.com/y3e5bb22) | [Python](https://tinyurl.com/wu6rdaw/1376_Time_Needed_to_Inform_All_Employees.py), [Swift](https://tinyurl.com/wuja3c4/1376_Time_Needed_to_Inform_All_Employees.swift) | [Art 1](https://tinyurl.com/y2lw6s4j) | Medium |📌 BFS, DFS |
616616
|49| [286. Walls and Gates](https://tinyurl.com/y3ollzsl) | [Python](https://tinyurl.com/wu6rdaw/286_Walls_and_Gates.py), [Swift](https://tinyurl.com/wuja3c4/286_Walls_and_Gates.swift) | --- | Medium |📌 BFS, DFS |
617617
|50| [286. Walls and Gates](https://tinyurl.com/y2xk6r98) | [Python](https://tinyurl.com/wu6rdaw/1138_Alphabet_Board_Path.py), [Swift](https://tinyurl.com/wuja3c4/1138_Alphabet_Board_Path.swift) | --- | Medium |📌 BFS |
618+
|51| [690. Employee Importance](https://tinyurl.com/yyweuk43) | [Python](https://tinyurl.com/wu6rdaw/690_Employee_Importance.py), [Swift](https://tinyurl.com/wuja3c4/690_Employee_Importance.swift) | --- | Easy |📌 BFS |
618619

619620

620621
</p>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for Employee.
3+
* public class Employee {
4+
* public var id: Int
5+
* public var importance: Int
6+
* public var subordinates: [Int]
7+
* public init(_ id: Int, _ importance: Int, _ subordinates: [Int]) {
8+
* self.id = id
9+
* self.importance = importance
10+
* self.subordinates = subordinates
11+
* }
12+
* }
13+
*/
14+
15+
import Foundation
16+
class Solution {
17+
func getImportance(_ employees: [Employee], _ id: Int) -> Int {
18+
var importance = 0
19+
var id_importance_map = [Int:Int]()
20+
var id_subordinates_map = [Int:Set<Int>]()
21+
22+
for employee in employees {
23+
id_importance_map[employee.id] = employee.importance
24+
id_subordinates_map[employee.id] = Set<Int>(employee.subordinates)
25+
}
26+
27+
var queue = [Int]()
28+
queue.append(id)
29+
var visited = Set<Int>()
30+
while !queue.isEmpty {
31+
let current_id = queue.removeFirst()
32+
importance += id_importance_map[current_id]!
33+
for subordinate in id_subordinates_map[current_id]! {
34+
if !visited.contains(subordinate) {
35+
visited.insert(subordinate)
36+
queue.append(subordinate)
37+
}
38+
}
39+
}
40+
41+
return importance
42+
}
43+
}

0 commit comments

Comments
 (0)