Skip to content

Commit 355400e

Browse files
authored
feat: add solutions to lc problems: No.0559,0563 (#3948)
1 parent 8b09d75 commit 355400e

File tree

17 files changed

+377
-186
lines changed

17 files changed

+377
-186
lines changed

solution/0500-0599/0559.Maximum Depth of N-ary Tree/README.md

+49-14
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ tags:
5959

6060
<!-- solution:start -->
6161

62-
### 方法一
62+
### 方法一:递归
63+
64+
我们首先判断 $\textit{root}$ 是否为空,若为空则返回 0。否则我们初始化一个变量 $\textit{mx}$ 用来记录子节点的最大深度,然后遍历 $\textit{root}$ 的所有子节点,递归调用 $\text{maxDepth}$ 函数,更新 $\textit{mx}$ 的值。最后返回 $\textit{mx} + 1$ 即可。
65+
66+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点的数量。
6367

6468
<!-- tabs:start -->
6569

@@ -69,17 +73,20 @@ tags:
6973
"""
7074
# Definition for a Node.
7175
class Node:
72-
def __init__(self, val=None, children=None):
76+
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
7377
self.val = val
7478
self.children = children
7579
"""
7680

7781

7882
class Solution:
79-
def maxDepth(self, root: 'Node') -> int:
83+
def maxDepth(self, root: "Node") -> int:
8084
if root is None:
8185
return 0
82-
return 1 + max([self.maxDepth(child) for child in root.children], default=0)
86+
mx = 0
87+
for child in root.children:
88+
mx = max(mx, self.maxDepth(child))
89+
return 1 + mx
8390
```
8491

8592
#### Java
@@ -109,11 +116,11 @@ class Solution {
109116
if (root == null) {
110117
return 0;
111118
}
112-
int ans = 1;
119+
int mx = 0;
113120
for (Node child : root.children) {
114-
ans = Math.max(ans, 1 + maxDepth(child));
121+
mx = Math.max(mx, maxDepth(child));
115122
}
116-
return ans;
123+
return 1 + mx;
117124
}
118125
}
119126
```
@@ -144,10 +151,14 @@ public:
144151
class Solution {
145152
public:
146153
int maxDepth(Node* root) {
147-
if (!root) return 0;
148-
int ans = 1;
149-
for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child));
150-
return ans;
154+
if (!root) {
155+
return 0;
156+
}
157+
int mx = 0;
158+
for (Node* child : root->children) {
159+
mx = max(mx, maxDepth(child));
160+
}
161+
return mx + 1;
151162
}
152163
};
153164
```
@@ -167,11 +178,35 @@ func maxDepth(root *Node) int {
167178
if root == nil {
168179
return 0
169180
}
170-
ans := 1
181+
mx := 0
171182
for _, child := range root.Children {
172-
ans = max(ans, 1+maxDepth(child))
183+
mx = max(mx, maxDepth(child))
173184
}
174-
return ans
185+
return 1 + mx
186+
}
187+
```
188+
189+
#### TypeScript
190+
191+
```ts
192+
/**
193+
* Definition for _Node.
194+
* class _Node {
195+
* val: number
196+
* children: _Node[]
197+
*
198+
* constructor(val?: number, children?: _Node[]) {
199+
* this.val = (val===undefined ? 0 : val)
200+
* this.children = (children===undefined ? [] : children)
201+
* }
202+
* }
203+
*/
204+
205+
function maxDepth(root: _Node | null): number {
206+
if (!root) {
207+
return 0;
208+
}
209+
return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0);
175210
}
176211
```
177212

solution/0500-0599/0559.Maximum Depth of N-ary Tree/README_EN.md

+49-14
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Recursion
61+
62+
First, we check if $\textit{root}$ is null. If it is, we return 0. Otherwise, we initialize a variable $\textit{mx}$ to record the maximum depth of the child nodes, then traverse all the child nodes of $\textit{root}$, recursively call the $\text{maxDepth}$ function, and update the value of $\textit{mx}$. Finally, we return $\textit{mx} + 1$.
63+
64+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes.
6165

6266
<!-- tabs:start -->
6367

@@ -67,17 +71,20 @@ tags:
6771
"""
6872
# Definition for a Node.
6973
class Node:
70-
def __init__(self, val=None, children=None):
74+
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
7175
self.val = val
7276
self.children = children
7377
"""
7478

7579

7680
class Solution:
77-
def maxDepth(self, root: 'Node') -> int:
81+
def maxDepth(self, root: "Node") -> int:
7882
if root is None:
7983
return 0
80-
return 1 + max([self.maxDepth(child) for child in root.children], default=0)
84+
mx = 0
85+
for child in root.children:
86+
mx = max(mx, self.maxDepth(child))
87+
return 1 + mx
8188
```
8289

8390
#### Java
@@ -107,11 +114,11 @@ class Solution {
107114
if (root == null) {
108115
return 0;
109116
}
110-
int ans = 1;
117+
int mx = 0;
111118
for (Node child : root.children) {
112-
ans = Math.max(ans, 1 + maxDepth(child));
119+
mx = Math.max(mx, maxDepth(child));
113120
}
114-
return ans;
121+
return 1 + mx;
115122
}
116123
}
117124
```
@@ -142,10 +149,14 @@ public:
142149
class Solution {
143150
public:
144151
int maxDepth(Node* root) {
145-
if (!root) return 0;
146-
int ans = 1;
147-
for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child));
148-
return ans;
152+
if (!root) {
153+
return 0;
154+
}
155+
int mx = 0;
156+
for (Node* child : root->children) {
157+
mx = max(mx, maxDepth(child));
158+
}
159+
return mx + 1;
149160
}
150161
};
151162
```
@@ -165,11 +176,35 @@ func maxDepth(root *Node) int {
165176
if root == nil {
166177
return 0
167178
}
168-
ans := 1
179+
mx := 0
169180
for _, child := range root.Children {
170-
ans = max(ans, 1+maxDepth(child))
181+
mx = max(mx, maxDepth(child))
171182
}
172-
return ans
183+
return 1 + mx
184+
}
185+
```
186+
187+
#### TypeScript
188+
189+
```ts
190+
/**
191+
* Definition for _Node.
192+
* class _Node {
193+
* val: number
194+
* children: _Node[]
195+
*
196+
* constructor(val?: number, children?: _Node[]) {
197+
* this.val = (val===undefined ? 0 : val)
198+
* this.children = (children===undefined ? [] : children)
199+
* }
200+
* }
201+
*/
202+
203+
function maxDepth(root: _Node | null): number {
204+
if (!root) {
205+
return 0;
206+
}
207+
return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0);
173208
}
174209
```
175210

solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class Node {
2121
class Solution {
2222
public:
2323
int maxDepth(Node* root) {
24-
if (!root) return 0;
25-
int ans = 1;
26-
for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child));
27-
return ans;
24+
if (!root) {
25+
return 0;
26+
}
27+
int mx = 0;
28+
for (Node* child : root->children) {
29+
mx = max(mx, maxDepth(child));
30+
}
31+
return mx + 1;
2832
}
29-
};
33+
};

solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ func maxDepth(root *Node) int {
1010
if root == nil {
1111
return 0
1212
}
13-
ans := 1
13+
mx := 0
1414
for _, child := range root.Children {
15-
ans = max(ans, 1+maxDepth(child))
15+
mx = max(mx, maxDepth(child))
1616
}
17-
return ans
18-
}
17+
return 1 + mx
18+
}

solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public int maxDepth(Node root) {
2222
if (root == null) {
2323
return 0;
2424
}
25-
int ans = 1;
25+
int mx = 0;
2626
for (Node child : root.children) {
27-
ans = Math.max(ans, 1 + maxDepth(child));
27+
mx = Math.max(mx, maxDepth(child));
2828
}
29-
return ans;
29+
return 1 + mx;
3030
}
31-
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
"""
22
# Definition for a Node.
33
class Node:
4-
def __init__(self, val=None, children=None):
4+
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
55
self.val = val
66
self.children = children
77
"""
88

99

1010
class Solution:
11-
def maxDepth(self, root: 'Node') -> int:
11+
def maxDepth(self, root: "Node") -> int:
1212
if root is None:
1313
return 0
14-
return 1 + max([self.maxDepth(child) for child in root.children], default=0)
14+
mx = 0
15+
for child in root.children:
16+
mx = max(mx, self.maxDepth(child))
17+
return 1 + mx
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for _Node.
3+
* class _Node {
4+
* val: number
5+
* children: _Node[]
6+
*
7+
* constructor(val?: number, children?: _Node[]) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.children = (children===undefined ? [] : children)
10+
* }
11+
* }
12+
*/
13+
14+
function maxDepth(root: _Node | null): number {
15+
if (!root) {
16+
return 0;
17+
}
18+
return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0);
19+
}

solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ tags:
6868
6969
遍历矩阵,当遇到 $1$ 时,更新 $f[i][j][k]$ 的值。对于每个位置 $(i, j)$,我们只需要更新其四个方向的值即可。然后更新答案。
7070

71-
时间复杂度 $O(m\times n)$,空间复杂度 $O(m\times n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
71+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
7272

7373
<!-- tabs:start -->
7474

solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ tags:
5454

5555
<!-- solution:start -->
5656

57-
### Solution 1
57+
### Solution 1: Dynamic Programming
58+
59+
We define $f[i][j][k]$ to represent the length of the longest consecutive $1$s ending at $(i, j)$ in direction $k$. The value range of $k$ is $0, 1, 2, 3$, representing horizontal, vertical, diagonal, and anti-diagonal directions, respectively.
60+
61+
> We can also use four 2D arrays to represent the length of the longest consecutive $1$s in the four directions.
62+
63+
We traverse the matrix, and when we encounter $1$, we update the value of $f[i][j][k]$. For each position $(i, j)$, we only need to update the values in its four directions. Then we update the answer.
64+
65+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively.
5866

5967
<!-- tabs:start -->
6068

0 commit comments

Comments
 (0)