-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
48 lines (45 loc) · 1.65 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace EmployeeImportance
{
class Employee
{
// It's the unique id of each node;
// unique id of this employee
public int id;
// the importance value of this employee
public int importance;
// the id of direct subordinates
public IList<int> subordinates;
}
class Program
{
static void Main(string[] args)
{
var employees = new List<Employee>();
employees.Add(new Employee {id = 1, importance = 5, subordinates = new List<int> {2, 3}});
employees.Add(new Employee {id = 2, importance = 3, subordinates = new List<int> {4}});
employees.Add(new Employee {id = 3, importance = 4, subordinates = new List<int> {}});
employees.Add(new Employee {id = 4, importance = 1, subordinates = new List<int> {}});
Console.WriteLine(GetImportance(employees, 1));
}
static int GetImportance(IList<Employee> employees, int id)
{
int importance = 0;
var dict = new Dictionary<int, Employee>();
foreach (var employee in employees)
dict.Add(employee.id, employee);
DFS(dict, id, ref importance);
return importance;
}
static void DFS(Dictionary<int, Employee> dict, int curId, ref int importance)
{
if(dict[curId] == null)
return;
importance += dict[curId].importance;
foreach (var subordinate in dict[curId].subordinates)
DFS(dict, subordinate, ref importance);
}
}
}