Throne Inheritance

本文介绍了一种用于确定王国中继承顺序的算法,通过定义递归函数Successor来获取家族树中下一个继承人的逻辑流程,并实现了一个ThroneInheritance类来管理继承顺序的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

思路:这题就是个DFS;

class ThroneInheritance {
    private HashMap<String, List<String>> hashmap;
    private HashSet<String> deadset;
    private String kingName;
    public ThroneInheritance(String kingName) {
        this.kingName = kingName;
        this.hashmap = new HashMap<>();
        this.deadset = new HashSet<>();
        hashmap.put(kingName, new ArrayList<String>());
    }
    
    public void birth(String parentName, String childName) {
        hashmap.putIfAbsent(parentName, new ArrayList<String>());
        hashmap.putIfAbsent(childName, new ArrayList<String>());
        hashmap.get(parentName).add(childName);
    }
    
    public void death(String name) {
        deadset.add(name);
    }
    
    public List<String> getInheritanceOrder() {
        List<String> fulllist = getInheritance(hashmap, kingName);
        return filterDeath(fulllist, deadset);
    }
    
    private List<String> getInheritance(HashMap<String, List<String>> hashmap, String start) {
        List<String> list = new ArrayList<String>();
        list.add(start);
        if(hashmap.get(start) != null && hashmap.get(start).size() > 0) {
            for(String child: hashmap.get(start)) {
                list.addAll(getInheritance(hashmap, child));
            }
        }
        return list;
    }
    
    private List<String> filterDeath(List<String> fulllist, HashSet<String> deadset) {
        List<String> res = new ArrayList<String>();
        for(String child: fulllist) {
            if(!deadset.contains(child)) {
                res.add(child);
            }
        }
        return res;
    }
}

/**
 * Your ThroneInheritance object will be instantiated and called as such:
 * ThroneInheritance obj = new ThroneInheritance(kingName);
 * obj.birth(parentName,childName);
 * obj.death(name);
 * List<String> param_3 = obj.getInheritanceOrder();
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值