Skip to content

Commit 906583a

Browse files
committed
1600. Throne Inheritance: AC
1 parent 533e1da commit 906583a

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1206,3 +1206,4 @@ mod s1594_maximum_non_negative_product_in_a_matrix;
12061206
mod s1595_minimum_cost_to_connect_two_groups_of_points;
12071207
mod s1598_crawler_log_folder;
12081208
mod s1599_maximum_profit_of_operating_a_centennial_wheel;
1209+
mod s1600_throne_inheritance;
+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
*/
61+
pub 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+
68+
struct ThroneInheritance {
69+
king: String,
70+
dead: std::collections::HashMap<String, bool>,
71+
children: std::collections::HashMap<String, Vec<String>>,
72+
}
73+
74+
/**
75+
* `&self` means the method takes an immutable reference.
76+
* If you need a mutable reference, change it to `&mut self` instead.
77+
*/
78+
impl ThroneInheritance {
79+
fn new(king_name: String) -> Self {
80+
Self {
81+
king: king_name,
82+
dead: std::collections::HashMap::new(),
83+
children: std::collections::HashMap::new(),
84+
}
85+
}
86+
87+
fn birth(&mut self, parent_name: String, child_name: String) {
88+
self.children
89+
.entry(parent_name)
90+
.or_insert_with(Vec::new)
91+
.push(child_name);
92+
}
93+
94+
fn death(&mut self, name: String) {
95+
self.dead.insert(name, true);
96+
}
97+
98+
fn get_inheritance_order(&self) -> Vec<String> {
99+
let mut order = Vec::new();
100+
self.dfs_helper(&self.king, &mut order);
101+
order
102+
}
103+
104+
fn dfs_helper(&self, name: &str, order: &mut Vec<String>) {
105+
if !self.dead.contains_key(name) {
106+
order.push(name.to_string());
107+
}
108+
if let Some(children) = self.children.get(name) {
109+
for child in children {
110+
self.dfs_helper(child, order);
111+
}
112+
}
113+
}
114+
}
115+
116+
/**
117+
* Your ThroneInheritance object will be instantiated and called as such:
118+
* let obj = ThroneInheritance::new(kingName);
119+
* obj.birth(parentName, childName);
120+
* obj.death(name);
121+
* let ret_3: Vec<String> = obj.get_inheritance_order();
122+
*/
123+
124+
// submission codes end
125+
126+
#[cfg(test)]
127+
mod tests {
128+
use super::*;
129+
130+
#[test]
131+
fn test_1600_example_1() {
132+
let mut t = ThroneInheritance::new("king".to_string()); // order: king
133+
t.birth("king".to_string(), "andy".to_string()); // order: king > andy
134+
t.birth("king".to_string(), "bob".to_string()); // order: king > andy > bob
135+
t.birth("king".to_string(), "catherine".to_string()); // order: king > andy > bob > catherine
136+
t.birth("andy".to_string(), "matthew".to_string()); // order: king > andy > matthew > bob > catherine
137+
t.birth("bob".to_string(), "alex".to_string()); // order: king > andy > matthew > bob > alex > catherine
138+
t.birth("bob".to_string(), "asha".to_string()); // order: king > andy > matthew > bob > alex > asha > catherine
139+
assert_eq!(
140+
t.get_inheritance_order(),
141+
vec_string![
142+
"king",
143+
"andy",
144+
"matthew",
145+
"bob",
146+
"alex",
147+
"asha",
148+
"catherine"
149+
]
150+
); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
151+
t.death("bob".to_string()); // order: king > andy > matthew > <s>bob</s> > alex > asha > catherine
152+
assert_eq!(
153+
t.get_inheritance_order(),
154+
vec_string!["king", "andy", "matthew", "alex", "asha", "catherine"]
155+
); // return ["king", "andy", "matthew", "alex", "asha", "catherine"]
156+
}
157+
}

0 commit comments

Comments
 (0)