{"id":7425,"date":"2020-09-27T01:11:05","date_gmt":"2020-09-27T08:11:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7425"},"modified":"2020-09-27T01:20:20","modified_gmt":"2020-09-27T08:20:20","slug":"leetcode-1600-throne-inheritance","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/recursion\/leetcode-1600-throne-inheritance\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1600. Throne Inheritance"},"content":{"rendered":"\n<p>A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born.<\/p>\n\n\n\n<p>The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let&#8217;s define the recursive function&nbsp;<code>Successor(x, curOrder)<\/code>, which given a person&nbsp;<code>x<\/code>&nbsp;and the inheritance order so far, returns who should be the next person after&nbsp;<code>x<\/code>&nbsp;in the order of inheritance.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Successor(x, curOrder):\n    if x has no children or all of x's children are in curOrder:\n        if x is the king return null\n        else return Successor(x's parent, curOrder)\n    else return x's oldest child who's not in curOrder\n<\/pre>\n\n\n\n<p>For example, assume we have a kingdom that consists of the king, his children Alice and Bob (Alice is older than Bob), and finally Alice&#8217;s son Jack.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>In the beginning,&nbsp;<code>curOrder<\/code>&nbsp;will be&nbsp;<code>[\"king\"]<\/code>.<\/li><li>Calling&nbsp;<code>Successor(king, curOrder)<\/code>&nbsp;will return Alice, so we append to&nbsp;<code>curOrder<\/code>&nbsp;to get&nbsp;<code>[\"king\", \"Alice\"]<\/code>.<\/li><li>Calling&nbsp;<code>Successor(Alice, curOrder)<\/code>&nbsp;will return Jack, so we append to&nbsp;<code>curOrder<\/code>&nbsp;to get&nbsp;<code>[\"king\", \"Alice\", \"Jack\"]<\/code>.<\/li><li>Calling&nbsp;<code>Successor(Jack, curOrder)<\/code>&nbsp;will return Bob, so we append to&nbsp;<code>curOrder<\/code>&nbsp;to get&nbsp;<code>[\"king\", \"Alice\", \"Jack\", \"Bob\"]<\/code>.<\/li><li>Calling&nbsp;<code>Successor(Bob, curOrder)<\/code>&nbsp;will return&nbsp;<code>null<\/code>. Thus the order of inheritance will be&nbsp;<code>[\"king\", \"Alice\", \"Jack\", \"Bob\"]<\/code>.<\/li><\/ol>\n\n\n\n<p>Using the above function, we can always obtain a unique order of inheritance.<\/p>\n\n\n\n<p>Implement the&nbsp;<code>ThroneInheritance<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>ThroneInheritance(string kingName)<\/code>&nbsp;Initializes an object of the&nbsp;<code>ThroneInheritance<\/code>&nbsp;class. The name of the king is given as part of the constructor.<\/li><li><code>void birth(string parentName, string childName)<\/code>&nbsp;Indicates that&nbsp;<code>parentName<\/code>&nbsp;gave birth to&nbsp;<code>childName<\/code>.<\/li><li><code>void death(string name)<\/code>&nbsp;Indicates the death of&nbsp;<code>name<\/code>. The death of the person doesn&#8217;t affect the&nbsp;<code>Successor<\/code>&nbsp;function nor the current inheritance order. You can treat it as just marking the person as dead.<\/li><li><code>string[] getInheritanceOrder()<\/code>&nbsp;Returns a list representing the current order of inheritance&nbsp;<strong>excluding<\/strong>&nbsp;dead people.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input<\/strong>\n[\"ThroneInheritance\", \"birth\", \"birth\", \"birth\", \"birth\", \"birth\", \"birth\", \"getInheritanceOrder\", \"death\", \"getInheritanceOrder\"]\n[[\"king\"], [\"king\", \"andy\"], [\"king\", \"bob\"], [\"king\", \"catherine\"], [\"andy\", \"matthew\"], [\"bob\", \"alex\"], [\"bob\", \"asha\"], [null], [\"bob\"], [null]]\n<strong>Output<\/strong>\n[null, null, null, null, null, null, null, [\"king\", \"andy\", \"matthew\", \"bob\", \"alex\", \"asha\", \"catherine\"], null, [\"king\", \"andy\", \"matthew\", \"alex\", \"asha\", \"catherine\"]]\n<strong>Explanation<\/strong>\nThroneInheritance t= new ThroneInheritance(\"king\"); \/\/ order: <strong>king<\/strong>\nt.birth(\"king\", \"andy\"); \/\/ order: king &gt; <strong>andy<\/strong>\nt.birth(\"king\", \"bob\"); \/\/ order: king &gt; andy &gt; <strong>bob<\/strong>\nt.birth(\"king\", \"catherine\"); \/\/ order: king &gt; andy &gt; bob &gt; <strong>catherine<\/strong>\nt.birth(\"andy\", \"matthew\"); \/\/ order: king &gt; andy &gt; <strong>matthew<\/strong> &gt; bob &gt; catherine\nt.birth(\"bob\", \"alex\"); \/\/ order: king &gt; andy &gt; matthew &gt; bob &gt; <strong>alex<\/strong> &gt; catherine\nt.birth(\"bob\", \"asha\"); \/\/ order: king &gt; andy &gt; matthew &gt; bob &gt; alex &gt; <strong>asha<\/strong> &gt; catherine\nt.getInheritanceOrder(); \/\/ return [\"king\", \"andy\", \"matthew\", \"bob\", \"alex\", \"asha\", \"catherine\"]\nt.death(\"bob\"); \/\/ order: king &gt; andy &gt; matthew &gt; <strong><s>bob<\/s><\/strong> &gt; alex &gt; asha &gt; catherine\nt.getInheritanceOrder(); \/\/ return [\"king\", \"andy\", \"matthew\", \"alex\", \"asha\", \"catherine\"]\n<\/p>\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= kingName.length, parentName.length, childName.length, name.length &lt;= 15<\/code><\/li><li><code>kingName<\/code>,&nbsp;<code>parentName<\/code>,&nbsp;<code>childName<\/code>, and&nbsp;<code>name<\/code>&nbsp;consist of lowercase English letters only.<\/li><li>All arguments&nbsp;<code>childName<\/code>&nbsp;and&nbsp;<code>kingName<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>.<\/li><li>All&nbsp;<code>name<\/code>&nbsp;arguments of&nbsp;<code>death<\/code>&nbsp;will be passed to either the constructor or as&nbsp;<code>childName<\/code>&nbsp;to&nbsp;<code>birth<\/code>&nbsp;first.<\/li><li>For each call to&nbsp;<code>birth(parentName, childName)<\/code>, it is guaranteed that&nbsp;<code>parentName<\/code>&nbsp;is alive.<\/li><li>At most&nbsp;<code>10<sup>5<\/sup><\/code>&nbsp;calls will be made to&nbsp;<code>birth<\/code>&nbsp;and&nbsp;<code>death<\/code>.<\/li><li>At most&nbsp;<code>10<\/code>&nbsp;calls will be made to&nbsp;<code>getInheritanceOrder<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable + DFS<\/strong><\/h2>\n\n\n\n<p>Record :<br>1. mapping from parent to children (ordered)<br>2. who has dead<\/p>\n\n\n\n<p>Time complexity: getInheritanceOrder O(n), other O(1)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass ThroneInheritance {\npublic:\n  ThroneInheritance(string kingName): king_(kingName) {\n    m_[kingName] = {};\n  }\n\n  void birth(string parentName, string childName) {\n    m_[parentName].push_back(childName);\n  }\n\n  void death(string name) {\n    dead_.insert(name);\n  }\n\n  vector<string> getInheritanceOrder() {\n    vector<string> ans;\n    function<void(const string&#038;)> dfs = [&](const string& name) {\n      if (!dead_.count(name)) ans.push_back(name);\n      for (const string& child: m_[name]) dfs(child);\n    };\n    dfs(king_);\n    return ans;\n  }\nprivate:  \n  string king_;  \n  unordered_map<string, vector<string>> m_; \/\/ parent -> list[children]\n  unordered_set<string> dead_;\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass ThroneInheritance:\n  def __init__(self, kingName: str):\n    self.kingName = kingName\n    self.family = defaultdict(list)    \n    self.dead = set()\n\n  def birth(self, parentName: str, childName: str) -> None:\n    self.family[parentName].append(childName)\n\n  def death(self, name: str) -> None:\n    self.dead.add(name)\n\n  def getInheritanceOrder(self) -> List[str]:\n    order = []\n    def dfs(name: str):\n      if name not in self.dead: order.append(name)\n      for child in self.family[name]: dfs(child)\n    dfs(self.kingName)\n    return order\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[33,82,177,17],"class_list":["post-7425","post","type-post","status-publish","format-standard","hentry","category-recursion","tag-dfs","tag-hashtable","tag-medium","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7425"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7425\/revisions"}],"predecessor-version":[{"id":7428,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7425\/revisions\/7428"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}