加载中...
1600-皇位继承顺序(Throne Inheritance)
发表于:2021-12-03 | 分类: 中等
字数统计: 2.5k | 阅读时长: 12分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/throne-inheritance

英文原文

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.

The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let's define the recursive function Successor(x, curOrder), which given a person x and the inheritance order so far, returns who should be the next person after x in the order of inheritance.

Successor(x, curOrder):
    if x has no children or all of x's children are in curOrder:
        if x is the king return null
        else return Successor(x's parent, curOrder)
    else return x's oldest child who's not in curOrder

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's son Jack.

  1. In the beginning, curOrder will be ["king"].
  2. Calling Successor(king, curOrder) will return Alice, so we append to curOrder to get ["king", "Alice"].
  3. Calling Successor(Alice, curOrder) will return Jack, so we append to curOrder to get ["king", "Alice", "Jack"].
  4. Calling Successor(Jack, curOrder) will return Bob, so we append to curOrder to get ["king", "Alice", "Jack", "Bob"].
  5. Calling Successor(Bob, curOrder) will return null. Thus the order of inheritance will be ["king", "Alice", "Jack", "Bob"].

Using the above function, we can always obtain a unique order of inheritance.

Implement the ThroneInheritance class:

  • ThroneInheritance(string kingName) Initializes an object of the ThroneInheritance class. The name of the king is given as part of the constructor.
  • void birth(string parentName, string childName) Indicates that parentName gave birth to childName.
  • void death(string name) Indicates the death of name. The death of the person doesn't affect the Successor function nor the current inheritance order. You can treat it as just marking the person as dead.
  • string[] getInheritanceOrder() Returns a list representing the current order of inheritance excluding dead people.

 

Example 1:

Input
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
Output
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]

Explanation
ThroneInheritance t= new ThroneInheritance("king"); // order: king
t.birth("king", "andy"); // order: king > andy
t.birth("king", "bob"); // order: king > andy > bob
t.birth("king", "catherine"); // order: king > andy > bob > catherine
t.birth("andy", "matthew"); // order: king > andy > matthew > bob > catherine
t.birth("bob", "alex"); // order: king > andy > matthew > bob > alex > catherine
t.birth("bob", "asha"); // order: king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // order: king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "alex", "asha", "catherine"]

 

Constraints:

  • 1 <= kingName.length, parentName.length, childName.length, name.length <= 15
  • kingName, parentName, childName, and name consist of lowercase English letters only.
  • All arguments childName and kingName are distinct.
  • All name arguments of death will be passed to either the constructor or as childName to birth first.
  • For each call to birth(parentName, childName), it is guaranteed that parentName is alive.
  • At most 105 calls will be made to birth and death.
  • At most 10 calls will be made to getInheritanceOrder.

中文题目

一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。

这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。

Successor(x, curOrder):
    如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中:
        如果 x 是国王,那么返回 null
        否则,返回 Successor(x 的父亲, curOrder)
    否则,返回 x 不在 curOrder 中最年长的孩子

比方说,假设王国由国王,他的孩子 Alice 和 Bob (Alice 比 Bob 年长)和 Alice 的孩子 Jack 组成。

  1. 一开始, curOrder 为 ["king"].
  2. 调用 Successor(king, curOrder) ,返回 Alice ,所以我们将 Alice 放入 curOrder 中,得到 ["king", "Alice"] 。
  3. 调用 Successor(Alice, curOrder) ,返回 Jack ,所以我们将 Jack 放入 curOrder 中,得到 ["king", "Alice", "Jack"] 。
  4. 调用 Successor(Jack, curOrder) ,返回 Bob ,所以我们将 Bob 放入 curOrder 中,得到 ["king", "Alice", "Jack", "Bob"] 。
  5. 调用 Successor(Bob, curOrder) ,返回 null 。最终得到继承顺序为 ["king", "Alice", "Jack", "Bob"] 。

通过以上的函数,我们总是能得到一个唯一的继承顺序。

请你实现 ThroneInheritance 类:

  • ThroneInheritance(string kingName) 初始化一个 ThroneInheritance 类的对象。国王的名字作为构造函数的参数传入。
  • void birth(string parentName, string childName) 表示 parentName 新拥有了一个名为 childName 的孩子。
  • void death(string name) 表示名为 name 的人死亡。一个人的死亡不会影响 Successor 函数,也不会影响当前的继承顺序。你可以只将这个人标记为死亡状态。
  • string[] getInheritanceOrder() 返回 除去 死亡人员的当前继承顺序列表。

 

示例:

输入:
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
输出:
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]

解释:
ThroneInheritance t= new ThroneInheritance("king"); // 继承顺序:king
t.birth("king", "andy"); // 继承顺序:king > andy
t.birth("king", "bob"); // 继承顺序:king > andy > bob
t.birth("king", "catherine"); // 继承顺序:king > andy > bob > catherine
t.birth("andy", "matthew"); // 继承顺序:king > andy > matthew > bob > catherine
t.birth("bob", "alex"); // 继承顺序:king > andy > matthew > bob > alex > catherine
t.birth("bob", "asha"); // 继承顺序:king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // 继承顺序:king > andy > matthew > bob(已经去世)> alex > asha > catherine
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "catherine"]

 

提示:

  • 1 <= kingName.length, parentName.length, childName.length, name.length <= 15
  • kingNameparentName, childName 和 name 仅包含小写英文字母。
  • 所有的参数 childName 和 kingName 互不相同
  • 所有 death 函数中的死亡名字 name 要么是国王,要么是已经出生了的人员名字。
  • 每次调用 birth(parentName, childName) 时,测试用例都保证 parentName 对应的人员是活着的。
  • 最多调用 105 次birth 和 death 。
  • 最多调用 10 次 getInheritanceOrder 。

通过代码

高赞题解

单向链表 & 标记删除

根据题意,我们需要将「新儿子」插入到「父亲」的「最后一个儿子」的「儿子们」的后面(注意这是个递归过程);如果该「父亲」还没有任何儿子,则直接插到「父亲」后面。

因此,我们需要在节点 Node 中使用一个 last 记录该节点的「最后一个儿子」,同时因为删除的时候,我们无法在 $O(1)$ 的复杂度内更新 last 信息,所以只能使用「标记删除」的方式。

image.png

代码:

[]
class ThroneInheritance { class Node { String name; Node next; Node last; // 记录最后一个儿子 boolean isDeleted = false; Node (String _name) { name = _name; } } Map<String, Node> map = new HashMap<>(); Node head = new Node(""), tail = new Node(""); public ThroneInheritance(String name) { Node root = new Node(name); root.next = tail; head.next = root; map.put(name, root); } public void birth(String pname, String cname) { Node node = new Node(cname); map.put(cname, node); Node p = map.get(pname); Node tmp = p; while (tmp.last != null) tmp = tmp.last; node.next = tmp.next; tmp.next = node; p.last = node; } public void death(String name) { Node node = map.get(name); node.isDeleted = true; } public List<String> getInheritanceOrder() { List<String> ans = new ArrayList<>(); Node tmp = head.next; while (tmp.next != null) { if (!tmp.isDeleted) ans.add(tmp.name); tmp = tmp.next; } return ans; } }
  • 时间复杂度:birthgetInheritanceOrder 操作为 $O(n)$;其余操作为 $O(1)$
  • 时间复杂度:$O(n)$

其他「链表」题解

题目 题解 难度 推荐指数
2. 两数相加 LeetCode 题解链接 中等 🤩🤩🤩
19. 删除链表的倒数第 N 个结点 LeetCode 题解链接 中等 🤩🤩🤩🤩🤩
21. 合并两个有序链表 LeetCode 题解链接 简单 🤩🤩🤩🤩🤩
23. 合并K个升序链表 LeetCode 题解链接 困难 🤩🤩🤩
24. 两两交换链表中的节点 LeetCode 题解链接 中等 🤩🤩🤩🤩
25. K 个一组翻转链表 LeetCode 题解链接 困难 🤩🤩
61. 旋转链表 LeetCode 题解链接 中等 🤩🤩🤩
83. 删除排序链表中的重复元素 LeetCode 题解链接 简单 🤩🤩🤩🤩🤩
82. 删除排序链表中的重复元素 II LeetCode 题解链接 简单 🤩🤩🤩🤩🤩
92. 反转链表 II LeetCode 题解链接 中等 🤩🤩🤩
160. 相交链表 LeetCode 题解链接 简单 🤩🤩🤩🤩🤩
146. LRU 缓存机制 LeetCode 题解链接 中等 🤩🤩🤩🤩🤩
203. 移除链表元素 LeetCode 题解链接 简单 🤩🤩🤩
460. LFU 缓存 LeetCode 题解链接 困难 🤩🤩🤩🤩🤩

最后

如果有帮助到你,请给题解点个赞和收藏,让更多的人看到 ~ (“▔□▔)/

也欢迎你 关注我 和 加入我们的「组队打卡」小群 ,提供写「证明」&「思路」的高质量题解。

所有题解已经加入 刷题指南,欢迎 star 哦 ~

统计信息

通过次数 提交次数 AC比率
17138 25317 67.7%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1599-经营摩天轮的最大利润(Maximum Profit of Operating a Centennial Wheel)
下一篇:
1601-最多可达成的换楼请求数目(Maximum Number of Achievable Transfer Requests)
本文目录
本文目录