-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path690.py
66 lines (64 loc) · 1.95 KB
/
690.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
__________________________________________________________________________________________________
sample 160 ms submission
"""
# Employee info
class Employee:
def __init__(self, id, importance, subordinates):
# It's the unique id of each node.
# unique id of this employee
self.id = id
# the importance value of this employee
self.importance = importance
# the id of direct subordinates
self.subordinates = subordinates
"""
class Solution:
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
hmap = {emp.id:emp for emp in employees}
lead = hmap[id]
stack = [lead]
res = 0
while len(stack)>0:
curr = stack.pop()
res += curr.importance
for sub in curr.subordinates:
stack.append(hmap[sub])
return res
__________________________________________________________________________________________________
sample 14072 kb submission
"""
# Employee info
class Employee:
def __init__(self, id, importance, subordinates):
# It's the unique id of each node.
# unique id of this employee
self.id = id
# the importance value of this employee
self.importance = importance
# the id of direct subordinates
self.subordinates = subordinates
"""
class Solution:
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
ret = 0
em = {}
for e in employees:
em[e.id] = e
q = [id]
while q:
c = em[q.pop(-1)]
ret += c.importance
for s in c.subordinates:
q.append(s)
return ret
__________________________________________________________________________________________________