|
| 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