Skip to content

Commit 4e4fd9f

Browse files
committed
Add solution #1600
1 parent 239e780 commit 4e4fd9f

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,410 LeetCode solutions in JavaScript
1+
# 1,411 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1235,6 +1235,7 @@
12351235
1595|[Minimum Cost to Connect Two Groups of Points](./solutions/1595-minimum-cost-to-connect-two-groups-of-points.js)|Hard|
12361236
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12371237
1599|[Maximum Profit of Operating a Centennial Wheel](./solutions/1599-maximum-profit-of-operating-a-centennial-wheel.js)|Medium|
1238+
1600|[Throne Inheritance](./solutions/1600-throne-inheritance.js)|Medium|
12381239
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12391240
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12401241
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|

solutions/1600-throne-inheritance.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* 1600. Throne Inheritance
3+
* https://leetcode.com/problems/throne-inheritance/
4+
* Difficulty: Medium
5+
*
6+
* A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while,
7+
* someone in the family dies or a child is born.
8+
*
9+
* The kingdom has a well-defined order of inheritance that consists of the king as the first
10+
* member. Let's define the recursive function Successor(x, curOrder), which given a person x and
11+
* the inheritance order so far, returns who should be the next person after x in the order of
12+
* inheritance.
13+
*
14+
* For example, assume we have a kingdom that consists of the king, his children Alice and Bob
15+
* (Alice is older than Bob), and finally Alice's son Jack.
16+
*
17+
* 1. In the beginning, curOrder will be ["king"].
18+
* 2. Calling Successor(king, curOrder) will return Alice, so we append to curOrder to
19+
* get ["king", "Alice"].
20+
* 3. Calling Successor(Alice, curOrder) will return Jack, so we append to curOrder to
21+
* get ["king", "Alice", "Jack"].
22+
* 4. Calling Successor(Jack, curOrder) will return Bob, so we append to curOrder to
23+
* get ["king", "Alice", "Jack", "Bob"].
24+
* 5. Calling Successor(Bob, curOrder) will return null. Thus the order of inheritance
25+
* will be ["king", "Alice", "Jack", "Bob"].
26+
*
27+
* Using the above function, we can always obtain a unique order of inheritance.
28+
*
29+
* Implement the ThroneInheritance class:
30+
* - ThroneInheritance(string kingName) Initializes an object of the ThroneInheritance class.
31+
* The name of the king is given as part of the constructor.
32+
* - void birth(string parentName, string childName) Indicates that parentName gave birth
33+
* to childName.
34+
* - void death(string name) Indicates the death of name. The death of the person doesn't
35+
* affect the Successor function nor the current inheritance order. You can treat it as
36+
* just marking the person as dead.
37+
* - string[] getInheritanceOrder() Returns a list representing the current order of inheritance
38+
* excluding dead people.
39+
*/
40+
41+
/**
42+
* @param {string} kingName
43+
*/
44+
var ThroneInheritance = function(kingName) {
45+
this.king = kingName;
46+
this.familyTree = new Map();
47+
this.deceased = new Set();
48+
};
49+
50+
/**
51+
* @param {string} parentName
52+
* @param {string} childName
53+
* @return {void}
54+
*/
55+
ThroneInheritance.prototype.birth = function(parentName, childName) {
56+
if (!this.familyTree.has(parentName)) {
57+
this.familyTree.set(parentName, []);
58+
}
59+
this.familyTree.get(parentName).push(childName);
60+
};
61+
62+
/**
63+
* @param {string} name
64+
* @return {void}
65+
*/
66+
ThroneInheritance.prototype.death = function(name) {
67+
this.deceased.add(name);
68+
};
69+
70+
/**
71+
* @return {string[]}
72+
*/
73+
ThroneInheritance.prototype.getInheritanceOrder = function() {
74+
const result = [];
75+
const traverse = (person) => {
76+
if (!this.deceased.has(person)) {
77+
result.push(person);
78+
}
79+
const children = this.familyTree.get(person) || [];
80+
for (const child of children) {
81+
traverse(child);
82+
}
83+
};
84+
traverse(this.king);
85+
return result;
86+
};

0 commit comments

Comments
 (0)