-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
690.cpp
82 lines (76 loc) · 2.22 KB
/
690.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
__________________________________________________________________________________________________
sample 28 ms submission
/*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/
class Solution {
public:
int calculate(unordered_map<int, Employee*>& emp_list, int id) {
auto emp = emp_list[id];
int imp = emp->importance;
for(int x: emp->subordinates) {
imp += calculate(emp_list, x);
}
return imp;
}
int getImportance(vector<Employee*> employees, int id) {
if(employees.empty())
return 0;
unordered_map<int, Employee*> emp_list;
transform(employees.begin(), employees.end(), inserter(emp_list, emp_list.end()), [](auto& a){return make_pair(a->id, a);});
return calculate(emp_list, id);
}
};
static auto s = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
__________________________________________________________________________________________________
sample 14112 kb submission
/*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/
class Solution {
public:
int get(vector<Employee*>& emp, int id) {
int res = 0;
int index = -1;
for (int i = 0; i < emp.size(); i++) {
if (emp[i]->id == id) {
index = i;
res = emp[i]->importance;
break;
}
}
for (int i = 0; i < emp[index]->subordinates.size(); i++) {
int subid = emp[index]->subordinates[i];
res += get(emp, subid);
}
return res;
}
int getImportance(vector<Employee*>& employees, int id) {
return get(employees, id);
}
};
__________________________________________________________________________________________________