-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-manager-hierarchy.java
74 lines (63 loc) · 2.06 KB
/
dev-manager-hierarchy.java
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
import java.util.*;
public class MyClass {
public static HashMap <String,Integer> result = new HashMap <String,Integer>();
public static void main(String[] args)
{
HashMap<String, String> dataSet = new HashMap<String, String>();
dataSet.put("A", "C");
dataSet.put("B", "C");
dataSet.put("C", "F");
dataSet.put("D", "E");
dataSet.put("E", "F");
dataSet.put("G", "F");
dataSet.put("F", "F");
solve(dataSet);
}
public static void solve(HashMap<String, String> dataset)
{
HashMap <String, LinkedList<String>> hm = new HashMap <String, LinkedList<String>>();
for(Map.Entry <String,String>e : dataset.entrySet())
{
//System.out.println(e.getValue() +" "+e.getKey());
String emp = e.getKey();
String man = e.getValue();
if(emp.equals(man))
continue;
LinkedList <String> temp = hm.get(man);
if(temp==null)
{
temp = new LinkedList<String>();
hm.put(man,temp);
}
temp.add(emp);
}
System.out.println(hm);
for(String emp : dataset.keySet())
getCount(emp,hm);
System.out.println(result);
}
public static int getCount(String emp, HashMap <String, LinkedList<String>> hm)
{
if(hm.containsKey(emp)==false)
{
result.put(emp,0);
return 0;
}
int count =0;
if(result.containsKey(emp))
return result.get(emp);
else
{
LinkedList <String> follow = hm.get(emp);
Iterator <String>itr = follow.iterator();
while(itr.hasNext())
{
String next = itr.next();
count = count+getCount(next,hm);
}
count = count+follow.size();
result.put(emp,count);
}
return count;
}
}