1600. Throne Inheritance Medium
1/**
2 * [1600] Throne Inheritance
3 *
4 * 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.
5 * 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.
6 *
7 * Successor(x, curOrder):
8 * if x has no children or all of x's children are in curOrder:
9 * if x is the king return null
10 * else return Successor(x's parent, curOrder)
11 * else return x's oldest child who's not in curOrder
12 *
13 * 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.
14 * <ol>
15 * In the beginning, curOrder will be ["king"].
16 * Calling Successor(king, curOrder) will return Alice, so we append to curOrder to get ["king", "Alice"].
17 * Calling Successor(Alice, curOrder) will return Jack, so we append to curOrder to get ["king", "Alice", "Jack"].
18 * Calling Successor(Jack, curOrder) will return Bob, so we append to curOrder to get ["king", "Alice", "Jack", "Bob"].
19 * Calling Successor(Bob, curOrder) will return null. Thus the order of inheritance will be ["king", "Alice", "Jack", "Bob"].
20 * </ol>
21 * Using the above function, we can always obtain a unique order of inheritance.
22 * Implement the ThroneInheritance class:
23 *
24 * ThroneInheritance(string kingName) Initializes an object of the ThroneInheritance class. The name of the king is given as part of the constructor.
25 * void birth(string parentName, string childName) Indicates that parentName gave birth to childName.
26 * 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.
27 * string[] getInheritanceOrder() Returns a list representing the current order of inheritance excluding dead people.
28 *
29 *
30 * Example 1:
31 *
32 * Input
33 * ["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
34 * [["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
35 * Output
36 * [null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
37 * Explanation
38 * ThroneInheritance t= new ThroneInheritance("king"); // order: king
39 * t.birth("king", "andy"); // order: king > andy
40 * t.birth("king", "bob"); // order: king > andy > bob
41 * t.birth("king", "catherine"); // order: king > andy > bob > catherine
42 * t.birth("andy", "matthew"); // order: king > andy > matthew > bob > catherine
43 * t.birth("bob", "alex"); // order: king > andy > matthew > bob > alex > catherine
44 * t.birth("bob", "asha"); // order: king > andy > matthew > bob > alex > asha > catherine
45 * t.getInheritanceOrder(); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
46 * t.death("bob"); // order: king > andy > matthew > <s>bob</s> > alex > asha > catherine
47 * t.getInheritanceOrder(); // return ["king", "andy", "matthew", "alex", "asha", "catherine"]
48 *
49 *
50 * Constraints:
51 *
52 * 1 <= kingName.length, parentName.length, childName.length, name.length <= 15
53 * kingName, parentName, childName, and name consist of lowercase English letters only.
54 * All arguments childName and kingName are distinct.
55 * All name arguments of death will be passed to either the constructor or as childName to birth first.
56 * For each call to birth(parentName, childName), it is guaranteed that parentName is alive.
57 * At most 10^5 calls will be made to birth and death.
58 * At most 10 calls will be made to getInheritanceOrder.
59 *
60 */
61pub struct Solution {}
62
63// problem: https://leetcode.com/problems/throne-inheritance/
64// discuss: https://leetcode.com/problems/throne-inheritance/discuss/?currentPage=1&orderBy=most_votes&query=
65
66// submission codes start here
67
68struct ThroneInheritance {
69 false
70 }
71
72
73/**
74 * `&self` means the method takes an immutable reference.
75 * If you need a mutable reference, change it to `&mut self` instead.
76 */
77impl ThroneInheritance {
78
79 fn new(kingName: String) -> Self {
80
81 }
82
83 fn birth(&self, parent_name: String, child_name: String) {
84
85 }
86
87 fn death(&self, name: String) {
88
89 }
90
91 fn get_inheritance_order(&self) -> Vec<String> {
92
93 }
94}
95
96/**
97 * Your ThroneInheritance object will be instantiated and called as such:
98 * let obj = ThroneInheritance::new(kingName);
99 * obj.birth(parentName, childName);
100 * obj.death(name);
101 * let ret_3: Vec<String> = obj.get_inheritance_order();
102 */
103
104// submission codes end
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn test_1600() {
112 }
113}
114
Back
© 2025 bowen.ge All Rights Reserved.