1
var bloodList = function (name, parent = null) {
2
return {
3
name,
4
children: [],
5
};
6
};
7

8
var ThroneInheritance = function (kingName) {
9
this.nameMap = new Map();
10
this.nameMap.set(kingName, bloodList(kingName));
11
this.king = kingName;
12
this.deadList = new Set();
13
};
14

15
/**
16
* @param {string} parentName
17
* @param {string} childName
18
* @return {void}
19
*/
20
ThroneInheritance.prototype.birth = function (parentName, childName) {
21
const parent = this.nameMap.get(parentName);
22
const childId = bloodList(childName, parent);
23
this.nameMap.set(childName, childId);
24
parent.children.push(childId);
25
};
26

27
/**
28
* @param {string} name
29
* @return {void}
30
*/
31
ThroneInheritance.prototype.death = function (name) {
32
this.deadList.add(name);
33
};
34

35
/**
36
* @return {string[]}
37
*/
38
function updateList(list, nameId, deadList) {
39
if (!deadList.has(nameId.name)) list.push(nameId.name);
40

41
for (let child of nameId.children) {
42
updateList(list, child, deadList);
43
}
44
}
45

46
ThroneInheritance.prototype.getInheritanceOrder = function () {
47
let list = [];
48
updateList(list, this.nameMap.get(this.king), this.deadList);
49
return list;
50
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0