Skip to content

Commit c28e1c0

Browse files
committed
Add solution 1600, change dir 0167、0303、0304、0307、0653、1017
1 parent fe99f9d commit c28e1c0

39 files changed

+758
-416
lines changed

README.md

+335-331
Large diffs are not rendered by default.

ctl/models/mdrow.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,37 @@ type Mdrow struct {
1818

1919
// GenerateMdRows define
2020
func GenerateMdRows(solutionIds []int, mdrows []Mdrow) {
21-
//fmt.Printf("solutionIds = %v\n\n", solutionIds)
21+
mdMap := map[int]Mdrow{}
22+
for _, row := range mdrows {
23+
mdMap[int(row.FrontendQuestionID)] = row
24+
}
2225
for i := 0; i < len(solutionIds); i++ {
23-
//fmt.Printf("solutionIds[i] = %v id = %v - %v\n", solutionIds[i], mdrows[solutionIds[i]].FrontendQuestionID, mdrows[solutionIds[i]].QuestionTitle)
24-
id := mdrows[solutionIds[i]-1].FrontendQuestionID
25-
if solutionIds[i] == int(id) {
26-
//fmt.Printf("id = %v i = %v solutionIds = %v\n", id, i, solutionIds[i])
27-
mdrows[id-1].SolutionPath = fmt.Sprintf("[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/%v)", fmt.Sprintf("%04d.%v", id, strings.Replace(strings.TrimSpace(mdrows[id-1].QuestionTitle), " ", "-", -1)))
26+
if row, ok := mdMap[solutionIds[i]]; ok {
27+
mdMap[solutionIds[i]] = Mdrow{
28+
FrontendQuestionID: row.FrontendQuestionID,
29+
QuestionTitle: row.QuestionTitle,
30+
QuestionTitleSlug: row.QuestionTitleSlug,
31+
SolutionPath: fmt.Sprintf("[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/%v)", fmt.Sprintf("%04d.%v", solutionIds[i], strings.Replace(strings.TrimSpace(row.QuestionTitle), " ", "-", -1))),
32+
Acceptance: row.Acceptance,
33+
Difficulty: row.Difficulty,
34+
Frequency: row.Frequency,
35+
}
2836
} else {
29-
fmt.Printf("序号出错了 len(solutionIds) = %v len(mdrows) = %v len(solutionIds) = %v solutionIds[i] = %v id = %v - %v\n", len(solutionIds), len(mdrows), len(solutionIds), solutionIds[i], id, mdrows[solutionIds[i]-1].QuestionTitle)
37+
fmt.Printf("序号不存在 len(solutionIds) = %v len(mdrows) = %v len(solutionIds) = %v solutionIds[i] = %v QuestionTitle = %v\n", len(solutionIds), len(mdrows), len(solutionIds), solutionIds[i], mdrows[solutionIds[i]-1].QuestionTitle)
38+
}
39+
}
40+
for i := range mdrows {
41+
mdrows[i] = Mdrow{
42+
FrontendQuestionID: mdrows[i].FrontendQuestionID,
43+
QuestionTitle: mdrows[i].QuestionTitle,
44+
QuestionTitleSlug: mdrows[i].QuestionTitleSlug,
45+
SolutionPath: mdMap[int(mdrows[i].FrontendQuestionID)].SolutionPath,
46+
Acceptance: mdrows[i].Acceptance,
47+
Difficulty: mdrows[i].Difficulty,
48+
Frequency: mdrows[i].Frequency,
3049
}
3150
}
51+
// fmt.Printf("mdrows = %v\n\n", mdrows)
3252
}
3353

3454
// | 0001 | Two Sum | [Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0001.Two-Sum)| 45.6% | Easy | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package leetcode
2+
3+
type ThroneInheritance struct {
4+
king string
5+
edges map[string][]string
6+
dead map[string]bool
7+
}
8+
9+
func Constructor(kingName string) (t ThroneInheritance) {
10+
return ThroneInheritance{kingName, map[string][]string{}, map[string]bool{}}
11+
}
12+
13+
func (t *ThroneInheritance) Birth(parentName, childName string) {
14+
t.edges[parentName] = append(t.edges[parentName], childName)
15+
}
16+
17+
func (t *ThroneInheritance) Death(name string) {
18+
t.dead[name] = true
19+
}
20+
21+
func (t *ThroneInheritance) GetInheritanceOrder() (res []string) {
22+
var preorder func(string)
23+
preorder = func(name string) {
24+
if !t.dead[name] {
25+
res = append(res, name)
26+
}
27+
for _, childName := range t.edges[name] {
28+
preorder(childName)
29+
}
30+
}
31+
preorder(t.king)
32+
return
33+
}
34+
35+
/**
36+
* Your ThroneInheritance object will be instantiated and called as such:
37+
* obj := Constructor(kingName);
38+
* obj.Birth(parentName,childName);
39+
* obj.Death(name);
40+
* param_3 := obj.GetInheritanceOrder();
41+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func Test_Problem1600(t *testing.T) {
9+
obj := Constructor("king")
10+
fmt.Printf("obj = %v\n", obj)
11+
obj.Birth("king", "andy")
12+
fmt.Printf("obj = %v\n", obj)
13+
obj.Birth("king", "bob")
14+
fmt.Printf("obj = %v\n", obj)
15+
obj.Birth("king", "catherine")
16+
fmt.Printf("obj = %v\n", obj)
17+
obj.Birth("andy", "matthew")
18+
fmt.Printf("obj = %v\n", obj)
19+
obj.Birth("bob", "alex")
20+
fmt.Printf("obj = %v\n", obj)
21+
obj.Birth("bob", "asha")
22+
fmt.Printf("obj = %v\n", obj)
23+
param2 := obj.GetInheritanceOrder()
24+
fmt.Printf("param_2 = %v obj = %v\n", param2, obj)
25+
obj.Death("bob")
26+
fmt.Printf("obj = %v\n", obj)
27+
param2 = obj.GetInheritanceOrder()
28+
fmt.Printf("param_2 = %v obj = %v\n", param2, obj)
29+
}
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# [1600. Throne Inheritance](https://leetcode.com/problems/throne-inheritance/)
2+
3+
4+
## 题目
5+
6+
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.
7+
8+
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.
9+
10+
```
11+
Successor(x, curOrder):
12+
if x has no children or all of x's children are in curOrder:
13+
if x is the king return null
14+
else return Successor(x's parent, curOrder)
15+
else return x's oldest child who's not in curOrder
16+
```
17+
18+
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.
19+
20+
1. In the beginning, `curOrder` will be `["king"]`.
21+
2. Calling `Successor(king, curOrder)` will return Alice, so we append to `curOrder` to get `["king", "Alice"]`.
22+
3. Calling `Successor(Alice, curOrder)` will return Jack, so we append to `curOrder` to get `["king", "Alice", "Jack"]`.
23+
4. Calling `Successor(Jack, curOrder)` will return Bob, so we append to `curOrder` to get `["king", "Alice", "Jack", "Bob"]`.
24+
5. Calling `Successor(Bob, curOrder)` will return `null`. Thus the order of inheritance will be `["king", "Alice", "Jack", "Bob"]`.
25+
26+
Using the above function, we can always obtain a unique order of inheritance.
27+
28+
Implement the `ThroneInheritance` class:
29+
30+
- `ThroneInheritance(string kingName)` Initializes an object of the `ThroneInheritance` class. The name of the king is given as part of the constructor.
31+
- `void birth(string parentName, string childName)` Indicates that `parentName` gave birth to `childName`.
32+
- `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.
33+
- `string[] getInheritanceOrder()` Returns a list representing the current order of inheritance **excluding** dead people.
34+
35+
**Example 1:**
36+
37+
```
38+
Input
39+
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
40+
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
41+
Output
42+
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
43+
44+
Explanation
45+
ThroneInheritance t= new ThroneInheritance("king"); // order:king
46+
t.birth("king", "andy"); // order: king >andy
47+
t.birth("king", "bob"); // order: king > andy >bob
48+
t.birth("king", "catherine"); // order: king > andy > bob >catherine
49+
t.birth("andy", "matthew"); // order: king > andy >matthew > bob > catherine
50+
t.birth("bob", "alex"); // order: king > andy > matthew > bob >alex > catherine
51+
t.birth("bob", "asha"); // order: king > andy > matthew > bob > alex >asha > catherine
52+
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
53+
t.death("bob"); // order: king > andy > matthew >bob > alex > asha > catherine
54+
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "alex", "asha", "catherine"]
55+
56+
```
57+
58+
**Constraints:**
59+
60+
- `1 <= kingName.length, parentName.length, childName.length, name.length <= 15`
61+
- `kingName``parentName``childName`, and `name` consist of lowercase English letters only.
62+
- All arguments `childName` and `kingName` are **distinct**.
63+
- All `name` arguments of `death` will be passed to either the constructor or as `childName` to `birth` first.
64+
- For each call to `birth(parentName, childName)`, it is guaranteed that `parentName` is alive.
65+
- At most `105` calls will be made to `birth` and `death`.
66+
- At most `10` calls will be made to `getInheritanceOrder`.
67+
68+
## 题目大意
69+
70+
一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。
71+
72+
## 解题思路
73+
74+
- 这道题思路不难。先将国王每个孩子按照顺序存在一个 map 中,然后每个国王的孩子还存在父子关系,同理也按顺序存在 map 中。执行 GetInheritanceOrder() 函数时,将国王的孩子按顺序遍历,如果每个孩子还有孩子,递归遍历到底。如果把继承关系看成一棵树,此题便是多叉树的先根遍历的问题。
75+
76+
## 代码
77+
78+
```go
79+
package leetcode
80+
81+
type ThroneInheritance struct {
82+
king string
83+
edges map[string][]string
84+
dead map[string]bool
85+
}
86+
87+
func Constructor(kingName string) (t ThroneInheritance) {
88+
return ThroneInheritance{kingName, map[string][]string{}, map[string]bool{}}
89+
}
90+
91+
func (t *ThroneInheritance) Birth(parentName, childName string) {
92+
t.edges[parentName] = append(t.edges[parentName], childName)
93+
}
94+
95+
func (t *ThroneInheritance) Death(name string) {
96+
t.dead[name] = true
97+
}
98+
99+
func (t *ThroneInheritance) GetInheritanceOrder() (res []string) {
100+
var preorder func(string)
101+
preorder = func(name string) {
102+
if !t.dead[name] {
103+
res = append(res, name)
104+
}
105+
for _, childName := range t.edges[name] {
106+
preorder(childName)
107+
}
108+
}
109+
preorder(t.king)
110+
return
111+
}
112+
113+
/**
114+
* Your ThroneInheritance object will be instantiated and called as such:
115+
* obj := Constructor(kingName);
116+
* obj.Birth(parentName,childName);
117+
* obj.Death(name);
118+
* param_3 := obj.GetInheritanceOrder();
119+
*/
120+
```

website/content/ChapterFour/1500~1599/1579.Remove-Max-Number-of-Edges-to-Keep-Graph-Fully-Traversable.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ func maxNumEdgesToRemove(n int, edges [][]int) int {
106106
----------------------------------------------
107107
<div style="display: flex;justify-content: space-between;align-items: center;">
108108
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1573.Number-of-Ways-to-Split-a-String/">⬅️上一页</a></p>
109-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1600~1699/1603.Design-Parking-System/">下一页➡️</a></p>
109+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1600~1699/1600.Throne-Inheritance/">下一页➡️</a></p>
110110
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# [1600. Throne Inheritance](https://leetcode.com/problems/throne-inheritance/)
2+
3+
4+
## 题目
5+
6+
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.
7+
8+
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.
9+
10+
```
11+
Successor(x, curOrder):
12+
if x has no children or all of x's children are in curOrder:
13+
if x is the king return null
14+
else return Successor(x's parent, curOrder)
15+
else return x's oldest child who's not in curOrder
16+
```
17+
18+
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.
19+
20+
1. In the beginning, `curOrder` will be `["king"]`.
21+
2. Calling `Successor(king, curOrder)` will return Alice, so we append to `curOrder` to get `["king", "Alice"]`.
22+
3. Calling `Successor(Alice, curOrder)` will return Jack, so we append to `curOrder` to get `["king", "Alice", "Jack"]`.
23+
4. Calling `Successor(Jack, curOrder)` will return Bob, so we append to `curOrder` to get `["king", "Alice", "Jack", "Bob"]`.
24+
5. Calling `Successor(Bob, curOrder)` will return `null`. Thus the order of inheritance will be `["king", "Alice", "Jack", "Bob"]`.
25+
26+
Using the above function, we can always obtain a unique order of inheritance.
27+
28+
Implement the `ThroneInheritance` class:
29+
30+
- `ThroneInheritance(string kingName)` Initializes an object of the `ThroneInheritance` class. The name of the king is given as part of the constructor.
31+
- `void birth(string parentName, string childName)` Indicates that `parentName` gave birth to `childName`.
32+
- `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.
33+
- `string[] getInheritanceOrder()` Returns a list representing the current order of inheritance **excluding** dead people.
34+
35+
**Example 1:**
36+
37+
```
38+
Input
39+
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
40+
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
41+
Output
42+
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
43+
44+
Explanation
45+
ThroneInheritance t= new ThroneInheritance("king"); // order:king
46+
t.birth("king", "andy"); // order: king >andy
47+
t.birth("king", "bob"); // order: king > andy >bob
48+
t.birth("king", "catherine"); // order: king > andy > bob >catherine
49+
t.birth("andy", "matthew"); // order: king > andy >matthew > bob > catherine
50+
t.birth("bob", "alex"); // order: king > andy > matthew > bob >alex > catherine
51+
t.birth("bob", "asha"); // order: king > andy > matthew > bob > alex >asha > catherine
52+
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
53+
t.death("bob"); // order: king > andy > matthew >bob > alex > asha > catherine
54+
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "alex", "asha", "catherine"]
55+
56+
```
57+
58+
**Constraints:**
59+
60+
- `1 <= kingName.length, parentName.length, childName.length, name.length <= 15`
61+
- `kingName``parentName``childName`, and `name` consist of lowercase English letters only.
62+
- All arguments `childName` and `kingName` are **distinct**.
63+
- All `name` arguments of `death` will be passed to either the constructor or as `childName` to `birth` first.
64+
- For each call to `birth(parentName, childName)`, it is guaranteed that `parentName` is alive.
65+
- At most `105` calls will be made to `birth` and `death`.
66+
- At most `10` calls will be made to `getInheritanceOrder`.
67+
68+
## 题目大意
69+
70+
一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。
71+
72+
## 解题思路
73+
74+
- 这道题思路不难。先将国王每个孩子按照顺序存在一个 map 中,然后每个国王的孩子还存在父子关系,同理也按顺序存在 map 中。执行 GetInheritanceOrder() 函数时,将国王的孩子按顺序遍历,如果每个孩子还有孩子,递归遍历到底。如果把继承关系看成一棵树,此题便是多叉树的先根遍历的问题。
75+
76+
## 代码
77+
78+
```go
79+
package leetcode
80+
81+
type ThroneInheritance struct {
82+
king string
83+
edges map[string][]string
84+
dead map[string]bool
85+
}
86+
87+
func Constructor(kingName string) (t ThroneInheritance) {
88+
return ThroneInheritance{kingName, map[string][]string{}, map[string]bool{}}
89+
}
90+
91+
func (t *ThroneInheritance) Birth(parentName, childName string) {
92+
t.edges[parentName] = append(t.edges[parentName], childName)
93+
}
94+
95+
func (t *ThroneInheritance) Death(name string) {
96+
t.dead[name] = true
97+
}
98+
99+
func (t *ThroneInheritance) GetInheritanceOrder() (res []string) {
100+
var preorder func(string)
101+
preorder = func(name string) {
102+
if !t.dead[name] {
103+
res = append(res, name)
104+
}
105+
for _, childName := range t.edges[name] {
106+
preorder(childName)
107+
}
108+
}
109+
preorder(t.king)
110+
return
111+
}
112+
113+
/**
114+
* Your ThroneInheritance object will be instantiated and called as such:
115+
* obj := Constructor(kingName);
116+
* obj.Birth(parentName,childName);
117+
* obj.Death(name);
118+
* param_3 := obj.GetInheritanceOrder();
119+
*/
120+
```
121+
122+
123+
----------------------------------------------
124+
<div style="display: flex;justify-content: space-between;align-items: center;">
125+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1579.Remove-Max-Number-of-Edges-to-Keep-Graph-Fully-Traversable/">⬅️上一页</a></p>
126+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1600~1699/1603.Design-Parking-System/">下一页➡️</a></p>
127+
</div>

website/content/ChapterFour/1600~1699/1603.Design-Parking-System.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ func (this *ParkingSystem) AddCar(carType int) bool {
105105

106106
----------------------------------------------
107107
<div style="display: flex;justify-content: space-between;align-items: center;">
108-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1579.Remove-Max-Number-of-Edges-to-Keep-Graph-Fully-Traversable/">⬅️上一页</a></p>
108+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1600~1699/1600.Throne-Inheritance/">⬅️上一页</a></p>
109109
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1600~1699/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X/">下一页➡️</a></p>
110110
</div>

0 commit comments

Comments
 (0)