Skip to content

Commit 7c19f5b

Browse files
committed
feat: add solutions to lc problem: No.1600
No.1600.Throne Inheritance
1 parent 1470cf9 commit 7c19f5b

File tree

6 files changed

+490
-1
lines changed

6 files changed

+490
-1
lines changed

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

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,193 @@ t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "
8181

8282
<!-- 这里可写通用的实现逻辑 -->
8383

84+
**方法一:多叉树的前序遍历**
85+
86+
可以发现,题目中王位的继承顺序,实际上是多叉树的前序遍历。
87+
88+
我们采用哈希表建树,得到 `g`,用哈希表 `dead` 保存死亡人员。获取继承顺序时,采用先序遍历的方式,把活着的人员放入结果数组中。
89+
90+
获取继承顺序的时间复杂度是 $O(n)$,其他操作的时间复杂度是 $O(1)$,空间复杂度 $O(n)$。其中 $n$ 是树中的节点数。
91+
8492
<!-- tabs:start -->
8593

8694
### **Python3**
8795

8896
<!-- 这里可写当前语言的特殊实现逻辑 -->
8997

9098
```python
91-
99+
class ThroneInheritance:
100+
101+
def __init__(self, kingName: str):
102+
self.g = defaultdict(list)
103+
self.dead = set()
104+
self.king = kingName
105+
106+
def birth(self, parentName: str, childName: str) -> None:
107+
self.g[parentName].append(childName)
108+
109+
def death(self, name: str) -> None:
110+
self.dead.add(name)
111+
112+
def getInheritanceOrder(self) -> List[str]:
113+
def dfs(x):
114+
if x not in self.dead:
115+
ans.append(x)
116+
for y in self.g[x]:
117+
dfs(y)
118+
119+
ans = []
120+
dfs(self.king)
121+
return ans
122+
123+
# Your ThroneInheritance object will be instantiated and called as such:
124+
# obj = ThroneInheritance(kingName)
125+
# obj.birth(parentName,childName)
126+
# obj.death(name)
127+
# param_3 = obj.getInheritanceOrder()
92128
```
93129

94130
### **Java**
95131

96132
<!-- 这里可写当前语言的特殊实现逻辑 -->
97133

98134
```java
135+
class ThroneInheritance {
136+
private Map<String, List<String>> g = new HashMap<>();
137+
private Set<String> dead = new HashSet<>();
138+
private List<String> ans;
139+
private String king;
140+
141+
public ThroneInheritance(String kingName) {
142+
king = kingName;
143+
}
144+
145+
public void birth(String parentName, String childName) {
146+
g.computeIfAbsent(parentName, k -> new ArrayList<>()).add(childName);
147+
}
148+
149+
public void death(String name) {
150+
dead.add(name);
151+
}
152+
153+
public List<String> getInheritanceOrder() {
154+
ans = new ArrayList<>();
155+
dfs(king);
156+
return ans;
157+
}
158+
159+
private void dfs(String x) {
160+
if (!dead.contains(x)) {
161+
ans.add(x);
162+
}
163+
for (String y : g.getOrDefault(x, Collections.emptyList())) {
164+
dfs(y);
165+
}
166+
}
167+
}
168+
169+
/**
170+
* Your ThroneInheritance object will be instantiated and called as such:
171+
* ThroneInheritance obj = new ThroneInheritance(kingName);
172+
* obj.birth(parentName,childName);
173+
* obj.death(name);
174+
* List<String> param_3 = obj.getInheritanceOrder();
175+
*/
176+
```
177+
178+
### **C++**
179+
180+
```cpp
181+
class ThroneInheritance {
182+
public:
183+
unordered_map<string, vector<string>> g;
184+
unordered_set<string> dead;
185+
string king;
186+
vector<string> ans;
187+
188+
ThroneInheritance(string kingName) {
189+
king = kingName;
190+
}
191+
192+
void birth(string parentName, string childName) {
193+
g[parentName].push_back(childName);
194+
}
195+
196+
void death(string name) {
197+
dead.insert(name);
198+
}
199+
200+
vector<string> getInheritanceOrder() {
201+
ans.resize(0);
202+
dfs(king);
203+
return ans;
204+
}
205+
206+
void dfs(string& x) {
207+
if (!dead.count(x)) {
208+
ans.push_back(x);
209+
}
210+
for (auto& y : g[x]) {
211+
dfs(y);
212+
}
213+
}
214+
};
215+
216+
/**
217+
* Your ThroneInheritance object will be instantiated and called as such:
218+
* ThroneInheritance* obj = new ThroneInheritance(kingName);
219+
* obj->birth(parentName,childName);
220+
* obj->death(name);
221+
* vector<string> param_3 = obj->getInheritanceOrder();
222+
*/
223+
```
99224
225+
### **Go**
226+
227+
```go
228+
type ThroneInheritance struct {
229+
g map[string][]string
230+
dead map[string]bool
231+
king string
232+
}
233+
234+
func Constructor(kingName string) ThroneInheritance {
235+
g := map[string][]string{}
236+
dead := map[string]bool{}
237+
return ThroneInheritance{g, dead, kingName}
238+
}
239+
240+
func (this *ThroneInheritance) Birth(parentName string, childName string) {
241+
this.g[parentName] = append(this.g[parentName], childName)
242+
}
243+
244+
func (this *ThroneInheritance) Death(name string) {
245+
this.dead[name] = true
246+
}
247+
248+
func (this *ThroneInheritance) GetInheritanceOrder() []string {
249+
var dfs func(x string)
250+
ans := []string{}
251+
252+
dfs = func(x string) {
253+
if !this.dead[x] {
254+
ans = append(ans, x)
255+
}
256+
for _, y := range this.g[x] {
257+
dfs(y)
258+
}
259+
}
260+
dfs(this.king)
261+
return ans
262+
}
263+
264+
/**
265+
* Your ThroneInheritance object will be instantiated and called as such:
266+
* obj := Constructor(kingName);
267+
* obj.Birth(parentName,childName);
268+
* obj.Death(name);
269+
* param_3 := obj.GetInheritanceOrder();
270+
*/
100271
```
101272

102273
### **...**

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

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,176 @@ t.getInheritanceOrder(); // return [&quot;king&quot;, &quot;andy&quot;, &quot;ma
8080
### **Python3**
8181

8282
```python
83+
class ThroneInheritance:
8384

85+
def __init__(self, kingName: str):
86+
self.g = defaultdict(list)
87+
self.dead = set()
88+
self.king = kingName
89+
90+
def birth(self, parentName: str, childName: str) -> None:
91+
self.g[parentName].append(childName)
92+
93+
def death(self, name: str) -> None:
94+
self.dead.add(name)
95+
96+
def getInheritanceOrder(self) -> List[str]:
97+
def dfs(x):
98+
if x not in self.dead:
99+
ans.append(x)
100+
for y in self.g[x]:
101+
dfs(y)
102+
103+
ans = []
104+
dfs(self.king)
105+
return ans
106+
107+
# Your ThroneInheritance object will be instantiated and called as such:
108+
# obj = ThroneInheritance(kingName)
109+
# obj.birth(parentName,childName)
110+
# obj.death(name)
111+
# param_3 = obj.getInheritanceOrder()
84112
```
85113

86114
### **Java**
87115

88116
```java
117+
class ThroneInheritance {
118+
private Map<String, List<String>> g = new HashMap<>();
119+
private Set<String> dead = new HashSet<>();
120+
private List<String> ans;
121+
private String king;
122+
123+
public ThroneInheritance(String kingName) {
124+
king = kingName;
125+
}
126+
127+
public void birth(String parentName, String childName) {
128+
g.computeIfAbsent(parentName, k -> new ArrayList<>()).add(childName);
129+
}
130+
131+
public void death(String name) {
132+
dead.add(name);
133+
}
134+
135+
public List<String> getInheritanceOrder() {
136+
ans = new ArrayList<>();
137+
dfs(king);
138+
return ans;
139+
}
140+
141+
private void dfs(String x) {
142+
if (!dead.contains(x)) {
143+
ans.add(x);
144+
}
145+
for (String y : g.getOrDefault(x, Collections.emptyList())) {
146+
dfs(y);
147+
}
148+
}
149+
}
150+
151+
/**
152+
* Your ThroneInheritance object will be instantiated and called as such:
153+
* ThroneInheritance obj = new ThroneInheritance(kingName);
154+
* obj.birth(parentName,childName);
155+
* obj.death(name);
156+
* List<String> param_3 = obj.getInheritanceOrder();
157+
*/
158+
```
159+
160+
### **C++**
161+
162+
```cpp
163+
class ThroneInheritance {
164+
public:
165+
unordered_map<string, vector<string>> g;
166+
unordered_set<string> dead;
167+
string king;
168+
vector<string> ans;
169+
170+
ThroneInheritance(string kingName) {
171+
king = kingName;
172+
}
173+
174+
void birth(string parentName, string childName) {
175+
g[parentName].push_back(childName);
176+
}
177+
178+
void death(string name) {
179+
dead.insert(name);
180+
}
181+
182+
vector<string> getInheritanceOrder() {
183+
ans.resize(0);
184+
dfs(king);
185+
return ans;
186+
}
187+
188+
void dfs(string& x) {
189+
if (!dead.count(x)) {
190+
ans.push_back(x);
191+
}
192+
for (auto& y : g[x]) {
193+
dfs(y);
194+
}
195+
}
196+
};
197+
198+
/**
199+
* Your ThroneInheritance object will be instantiated and called as such:
200+
* ThroneInheritance* obj = new ThroneInheritance(kingName);
201+
* obj->birth(parentName,childName);
202+
* obj->death(name);
203+
* vector<string> param_3 = obj->getInheritanceOrder();
204+
*/
205+
```
206+
207+
### **Go**
208+
209+
```go
210+
type ThroneInheritance struct {
211+
g map[string][]string
212+
dead map[string]bool
213+
king string
214+
}
215+
216+
func Constructor(kingName string) ThroneInheritance {
217+
g := map[string][]string{}
218+
dead := map[string]bool{}
219+
return ThroneInheritance{g, dead, kingName}
220+
}
221+
222+
func (this *ThroneInheritance) Birth(parentName string, childName string) {
223+
this.g[parentName] = append(this.g[parentName], childName)
224+
}
225+
226+
func (this *ThroneInheritance) Death(name string) {
227+
this.dead[name] = true
228+
}
229+
230+
func (this *ThroneInheritance) GetInheritanceOrder() []string {
231+
var dfs func(x string)
232+
ans := []string{}
233+
234+
dfs = func(x string) {
235+
if !this.dead[x] {
236+
ans = append(ans, x)
237+
}
238+
for _, y := range this.g[x] {
239+
dfs(y)
240+
}
241+
}
242+
dfs(this.king)
243+
return ans
244+
}
89245
246+
/**
247+
* Your ThroneInheritance object will be instantiated and called as such:
248+
* obj := Constructor(kingName);
249+
* obj.Birth(parentName,childName);
250+
* obj.Death(name);
251+
* param_3 := obj.GetInheritanceOrder();
252+
*/
90253
```
91254

92255
### **...**

0 commit comments

Comments
 (0)