【LeetCode】 1600. Throne Inheritance 皇位继承顺序(Medium)(JAVA)

【LeetCode】 1600. Throne Inheritance 皇位继承顺序(Medium)(JAVA)

题目地址: https://leetcode.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:

  1. ThroneInheritance(string kingName) Initializes an object of the ThroneInheritance class. The name of the king is given as part of the constructor.
  2. void birth(string parentName, string childName) Indicates that parentName gave birth to childName.
  3. 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.
  4. 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. 1 <= kingName.length, parentName.length, childName.length, name.length <= 15
  2. kingName, parentName, childName, and name consist of lowercase English letters only.
  3. All arguments childName and kingName are distinct.
  4. All name arguments of death will be passed to either the constructor or as childName to birth first.
  5. For each call to birth(parentName, childName), it is guaranteed that parentName is alive.
  6. At most 105 calls will be made to birth and death.
  7. 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 类:

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

解题方法

  1. 这题其实就是多叉树的前序遍历
  2. 首先构造一个多叉树 ThroneTree
  3. ThroneInheritance 构造函数,创建一个 head; birth 记录 ThroneTree 里面的 childens; death 标记 ThroneTree 死亡; getInheritanceOrder 前序遍历找出王位继承者
class ThroneInheritance {

    public class ThroneTree {

        public List<ThroneTree> childens = new ArrayList<>();
        public final String kingName;
        public boolean die = false;

        public ThroneTree(String kingName) {
            this.kingName = kingName;
        }

        public void makeDeath() {
            die = true;
        }

        public void addChild(ThroneTree throneTree) {
            childens.add(throneTree);
        }
    }

    public ThroneTree king;
    public Map<String, ThroneTree> map = new HashMap<>();

    public ThroneInheritance(String kingName) {
        king = new ThroneTree(kingName);
        map.put(kingName, king);
    }
    
    public void birth(String parentName, String childName) {
        if (map.get(parentName) == null) return;
        ThroneTree child = new ThroneTree(childName);
        map.put(childName, child);
        map.get(parentName).addChild(child);
    }
    
    public void death(String name) {
        if (map.get(name) == null) return;
        map.get(name).makeDeath();
    }
    
    public List<String> getInheritanceOrder() {
        List<String> res = new ArrayList<>();
        getInheritanceOrder(res, king);
        return res;
    }
    
    private void getInheritanceOrder(List<String> res, ThroneTree king) {
        if (!king.die) res.add(king.kingName);
        if (king.childens.size() == 0) return;
        for (ThroneTree child: king.childens) {
            getInheritanceOrder(res, child);
        }
    }
}

执行耗时:305 ms,击败了98.33% 的Java用户
内存消耗:96.4 MB,击败了92.78% 的Java用户

欢迎关注我的公众号,LeetCode 每日一题更新
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值