-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
41 lines (37 loc) · 1.12 KB
/
solution.ts
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
class ThroneInheritance {
private dSet = new Set<string>();
private p2cMap = new Map<string, string[]>();
constructor (private kingName: string) {
this.p2cMap.set(kingName, []);
}
birth (parentName: string, childName: string): void {
this.p2cMap.get(parentName)!.push(childName);
this.p2cMap.set(childName, []);
}
death (name: string): void {
this.dSet.add(name);
}
getInheritanceOrder (): string[] {
const map = this.p2cMap;
const set = this.dSet;
const result:string[] = [];
const dfs = (pName:string) => {
if (!set.has(pName)) {
result.push(pName);
}
const children = map.get(pName)!;
for (let i = 0; i < children.length; i++) {
dfs(children[i]);
}
};
dfs(this.kingName);
return result;
}
}
/**
* Your ThroneInheritance object will be instantiated and called as such:
* var obj = new ThroneInheritance(kingName)
* obj.birth(parentName,childName)
* obj.death(name)
* var param_3 = obj.getInheritanceOrder()
*/