Skip to content

Commit 5812f34

Browse files
authored
feat: add solutions to lc problem: No.1600 (doocs#2542)
No.1600.Throne Inheritance
1 parent 89db25c commit 5812f34

File tree

8 files changed

+377
-91
lines changed

8 files changed

+377
-91
lines changed

solution/1600-1699/1600.Throne Inheritance/README.md

+128-32
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,23 @@ t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "
8383

8484
### 方法一:多叉树的前序遍历
8585

86-
可以发现,题目中王位的继承顺序,实际上是多叉树的前序遍历
86+
根据题目描述,我们可以发现,王位继承顺序实际上是一个多叉树的前序遍历。我们可以使用一个哈希表 $g$ 存储每个人的孩子,使用一个集合 $dead$ 存储已经去世的人
8787

88-
我们采用哈希表建树,得到 `g`,用哈希表 `dead` 保存死亡人员。获取继承顺序时,采用先序遍历的方式,把活着的人员放入结果数组中。
88+
- 调用 `birth(parentName, childName)` 时,我们将 `childName` 添加到 `parentName` 的孩子列表中。
89+
- 调用 `death(name)` 时,我们将 `name` 添加到 `dead` 集合中。
90+
- 调用 `getInheritanceOrder()` 时,我们从国王开始进行深度优先搜索,如果当前节点 `x` 没有去世,我们将 `x` 添加到答案列表中,然后递归地遍历 `x` 的所有孩子。
8991

90-
获取继承顺序的时间复杂度是 $O(n)$,其他操作的时间复杂度是 $O(1)$,空间复杂度 $O(n)$。其中 $n$ 是树中的节点数
92+
时间复杂度方面,`birth``death` 的时间复杂度均为 $O(1)$,`getInheritanceOrder` 的时间复杂度为 $O(n)$,空间复杂度为 $O(n)$。其中 $n$ 是节点数量
9193

9294
<!-- tabs:start -->
9395

9496
```python
9597
class ThroneInheritance:
98+
9699
def __init__(self, kingName: str):
97-
self.g = defaultdict(list)
98-
self.dead = set()
99100
self.king = kingName
101+
self.dead = set()
102+
self.g = defaultdict(list)
100103

101104
def birth(self, parentName: str, childName: str) -> None:
102105
self.g[parentName].append(childName)
@@ -105,9 +108,8 @@ class ThroneInheritance:
105108
self.dead.add(name)
106109

107110
def getInheritanceOrder(self) -> List[str]:
108-
def dfs(x):
109-
if x not in self.dead:
110-
ans.append(x)
111+
def dfs(x: str):
112+
x not in self.dead and ans.append(x)
111113
for y in self.g[x]:
112114
dfs(y)
113115

@@ -125,10 +127,10 @@ class ThroneInheritance:
125127

126128
```java
127129
class ThroneInheritance {
128-
private Map<String, List<String>> g = new HashMap<>();
129-
private Set<String> dead = new HashSet<>();
130-
private List<String> ans;
131130
private String king;
131+
private Set<String> dead = new HashSet<>();
132+
private Map<String, List<String>> g = new HashMap<>();
133+
private List<String> ans = new ArrayList<>();
132134

133135
public ThroneInheritance(String kingName) {
134136
king = kingName;
@@ -143,7 +145,7 @@ class ThroneInheritance {
143145
}
144146

145147
public List<String> getInheritanceOrder() {
146-
ans = new ArrayList<>();
148+
ans.clear();
147149
dfs(king);
148150
return ans;
149151
}
@@ -152,7 +154,7 @@ class ThroneInheritance {
152154
if (!dead.contains(x)) {
153155
ans.add(x);
154156
}
155-
for (String y : g.getOrDefault(x, Collections.emptyList())) {
157+
for (String y : g.getOrDefault(x, List.of())) {
156158
dfs(y);
157159
}
158160
}
@@ -170,17 +172,12 @@ class ThroneInheritance {
170172
```cpp
171173
class ThroneInheritance {
172174
public:
173-
unordered_map<string, vector<string>> g;
174-
unordered_set<string> dead;
175-
string king;
176-
vector<string> ans;
177-
178175
ThroneInheritance(string kingName) {
179176
king = kingName;
180177
}
181178

182179
void birth(string parentName, string childName) {
183-
g[parentName].push_back(childName);
180+
g[parentName].emplace_back(childName);
184181
}
185182

186183
void death(string name) {
@@ -193,9 +190,15 @@ public:
193190
return ans;
194191
}
195192

193+
private:
194+
string king;
195+
unordered_set<string> dead;
196+
unordered_map<string, vector<string>> g;
197+
vector<string> ans;
198+
196199
void dfs(string& x) {
197-
if (!dead.count(x)) {
198-
ans.push_back(x);
200+
if (!dead.contains(x)) {
201+
ans.emplace_back(x);
199202
}
200203
for (auto& y : g[x]) {
201204
dfs(y);
@@ -214,29 +217,29 @@ public:
214217
215218
```go
216219
type ThroneInheritance struct {
217-
g map[string][]string
218-
dead map[string]bool
219220
king string
221+
dead map[string]bool
222+
g map[string][]string
220223
}
221224
225+
222226
func Constructor(kingName string) ThroneInheritance {
223-
g := map[string][]string{}
224-
dead := map[string]bool{}
225-
return ThroneInheritance{g, dead, kingName}
227+
return ThroneInheritance{kingName, map[string]bool{}, map[string][]string{}}
226228
}
227229
228-
func (this *ThroneInheritance) Birth(parentName string, childName string) {
230+
231+
func (this *ThroneInheritance) Birth(parentName string, childName string) {
229232
this.g[parentName] = append(this.g[parentName], childName)
230233
}
231234
232-
func (this *ThroneInheritance) Death(name string) {
235+
236+
func (this *ThroneInheritance) Death(name string) {
233237
this.dead[name] = true
234238
}
235239
236-
func (this *ThroneInheritance) GetInheritanceOrder() []string {
237-
var dfs func(x string)
238-
ans := []string{}
239240
241+
func (this *ThroneInheritance) GetInheritanceOrder() (ans []string) {
242+
var dfs func(string)
240243
dfs = func(x string) {
241244
if !this.dead[x] {
242245
ans = append(ans, x)
@@ -246,9 +249,10 @@ func (this *ThroneInheritance) GetInheritanceOrder() []string {
246249
}
247250
}
248251
dfs(this.king)
249-
return ans
252+
return
250253
}
251254
255+
252256
/**
253257
* Your ThroneInheritance object will be instantiated and called as such:
254258
* obj := Constructor(kingName);
@@ -258,6 +262,98 @@ func (this *ThroneInheritance) GetInheritanceOrder() []string {
258262
*/
259263
```
260264

265+
```ts
266+
class ThroneInheritance {
267+
private king: string;
268+
private dead: Set<string> = new Set();
269+
private g: Map<string, string[]> = new Map();
270+
271+
constructor(kingName: string) {
272+
this.king = kingName;
273+
}
274+
275+
birth(parentName: string, childName: string): void {
276+
this.g.set(parentName, this.g.get(parentName) || []);
277+
this.g.get(parentName)!.push(childName);
278+
}
279+
280+
death(name: string): void {
281+
this.dead.add(name);
282+
}
283+
284+
getInheritanceOrder(): string[] {
285+
const ans: string[] = [];
286+
const dfs = (x: string) => {
287+
if (!this.dead.has(x)) {
288+
ans.push(x);
289+
}
290+
for (const y of this.g.get(x) || []) {
291+
dfs(y);
292+
}
293+
};
294+
dfs(this.king);
295+
return ans;
296+
}
297+
}
298+
299+
/**
300+
* Your ThroneInheritance object will be instantiated and called as such:
301+
* var obj = new ThroneInheritance(kingName)
302+
* obj.birth(parentName,childName)
303+
* obj.death(name)
304+
* var param_3 = obj.getInheritanceOrder()
305+
*/
306+
```
307+
308+
```cs
309+
public class ThroneInheritance {
310+
private string king;
311+
private HashSet<string> dead = new HashSet<string>();
312+
private Dictionary<string, List<string>> g = new Dictionary<string, List<string>>();
313+
private List<string> ans = new List<string>();
314+
315+
public ThroneInheritance(string kingName) {
316+
king = kingName;
317+
}
318+
319+
public void Birth(string parentName, string childName) {
320+
if (!g.ContainsKey(parentName)) {
321+
g[parentName] = new List<string>();
322+
}
323+
g[parentName].Add(childName);
324+
}
325+
326+
public void Death(string name) {
327+
dead.Add(name);
328+
}
329+
330+
public IList<string> GetInheritanceOrder() {
331+
ans.Clear();
332+
DFS(king);
333+
return ans;
334+
}
335+
336+
private void DFS(string x) {
337+
if (!dead.Contains(x)) {
338+
ans.Add(x);
339+
}
340+
if (g.ContainsKey(x)) {
341+
foreach (string y in g[x]) {
342+
DFS(y);
343+
}
344+
}
345+
}
346+
}
347+
348+
/**
349+
* Your ThroneInheritance object will be instantiated and called as such:
350+
* ThroneInheritance obj = new ThroneInheritance(kingName);
351+
* obj.Birth(parentName,childName);
352+
* obj.Death(name);
353+
* IList<string> param_3 = obj.GetInheritanceOrder();
354+
*/
355+
```
356+
261357
<!-- tabs:end -->
262358

263359
<!-- end -->

0 commit comments

Comments
 (0)