每日一题 王位继承顺序
一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。
这个王国有一个明确规定的王位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder)
,给定一个人 x
和当前的继承顺序,该函数返回 x
的下一继承人。
Successor(x, curOrder): 如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中: 如果 x 是国王,那么返回 null 否则,返回 Successor(x 的父亲, curOrder) 否则,返回 x 不在 curOrder 中最年长的孩子
比方说,假设王国由国王,他的孩子 Alice 和 Bob (Alice 比 Bob 年长)和 Alice 的孩子 Jack 组成。
- 一开始,
curOrder
为["king"]
. - 调用
Successor(king, curOrder)
,返回 Alice ,所以我们将 Alice 放入curOrder
中,得到["king", "Alice"]
。 - 调用
Successor(Alice, curOrder)
,返回 Jack ,所以我们将 Jack 放入curOrder
中,得到["king", "Alice", "Jack"]
。 - 调用
Successor(Jack, curOrder)
,返回 Bob ,所以我们将 Bob 放入curOrder
中,得到["king", "Alice", "Jack", "Bob"]
。 - 调用
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"]
解题思路
从Successor函数可以看出,继承人的顺序可以看做是多叉树的顺序遍历,因此,题目可以近似为让我们构建并顺序遍历一个多叉树。使用应该元组存储死亡列表,用字典存储一个父亲(key)的所有儿子(value,用列表表示)。
代码实现
from typing import List
class ThroneInheritance:
def __init__(self, kingName: str):
self.kingName = kingName
self.son = {kingName: []}
self.deathList = set()
def birth(self, parentName: str, childName: str) -> None:
self.son[parentName].append(childName)
self.son[childName] = []
def death(self, name: str) -> None:
self.deathList.add(name)
def getInheritanceOrder(self) -> List[str]:
self.successor = []
def getson(name):
if name not in self.deathList:
self.successor.append(name)
if name in self.son:
for son in self.son[name]:
getson(son)
getson(self.kingName)
return self.successor
复杂度分析
时间:初始化、birth、death均为O(1),getInheritanceOrder为O(n)
空间:O(n)