Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ce2a9c2

Browse files
feat: add weekly contest 410 (#3401)
1 parent dc4a9d5 commit ce2a9c2

File tree

37 files changed

+2611
-2
lines changed

37 files changed

+2611
-2
lines changed

‎solution/1000-1099/1035.Uncrossed Lines/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tags:
3939
<pre>
4040
<strong>输入:</strong>nums1 = <span id="example-input-1-1">[1,4,2]</span>, nums2 = <span id="example-input-1-2">[1,2,4]</span>
4141
<strong>输出:</strong><span id="example-output-1">2</span>
42-
<strong>解释:</strong>可以画出两条不交叉的线,如上图所示。
42+
<strong>解释:</strong>可以画出两条不交叉的线,如上图所示。
4343
但无法画出第三条不相交的直线,因为从 nums1[1]=4 到 nums2[2]=4 的直线将与从 nums1[2]=2 到 nums2[1]=2 的直线相交。
4444
</pre>
4545

‎solution/3200-3299/3248.Snake in Matrix/README.md‎

Lines changed: 293 additions & 0 deletions
Large diffs are not rendered by default.

‎solution/3200-3299/3248.Snake in Matrix/README_EN.md‎

Lines changed: 291 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int finalPositionOfSnake(int n, vector<string>& commands) {
4+
int x = 0, y = 0;
5+
for (const auto& c : commands) {
6+
switch (c[0]) {
7+
case 'U': x--; break;
8+
case 'D': x++; break;
9+
case 'L': y--; break;
10+
case 'R': y++; break;
11+
}
12+
}
13+
return x * n + y;
14+
}
15+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func finalPositionOfSnake(n int, commands []string) int {
2+
x, y := 0, 0
3+
for _, c := range commands {
4+
switch c[0] {
5+
case 'U':
6+
x--
7+
case 'D':
8+
x++
9+
case 'L':
10+
y--
11+
case 'R':
12+
y++
13+
}
14+
}
15+
return x*n + y
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int finalPositionOfSnake(int n, List<String> commands) {
3+
int x = 0, y = 0;
4+
for (var c : commands) {
5+
switch (c.charAt(0)) {
6+
case 'U' -> x--;
7+
case 'D' -> x++;
8+
case 'L' -> y--;
9+
case 'R' -> y++;
10+
}
11+
}
12+
return x * n + y;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def finalPositionOfSnake(self, n: int, commands: List[str]) -> int:
3+
x = y = 0
4+
for c in commands:
5+
match c[0]:
6+
case "U":
7+
x -= 1
8+
case "D":
9+
x += 1
10+
case "L":
11+
y -= 1
12+
case "R":
13+
y += 1
14+
return x * n + y
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function finalPositionOfSnake(n: number, commands: string[]): number {
2+
let [x, y] = [0, 0];
3+
for (const c of commands) {
4+
c[0] === 'U' && x--;
5+
c[0] === 'D' && x++;
6+
c[0] === 'L' && y--;
7+
c[0] === 'R' && y++;
8+
}
9+
return x * n + y;
10+
}
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3249.Count%20the%20Number%20of%20Good%20Nodes/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3249. 统计好节点的数目](https://leetcode.cn/problems/count-the-number-of-good-nodes)
10+
11+
[English Version](/solution/3200-3299/3249.Count%20the%20Number%20of%20Good%20Nodes/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>现有一棵 <strong>无向</strong> 树,树中包含 <code>n</code> 个节点,按从 <code>0</code> 到 <code>n - 1</code> 标记。树的根节点是节点 <code>0</code> 。给你一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code>,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示树中节点 <code>a<sub>i</sub></code> 与节点 <code>b<sub>i</sub></code> 之间存在一条边。</p>
18+
19+
<p>如果一个节点的所有子节点为根的&nbsp;<span data-keyword="subtree">子树</span>&nbsp;包含的节点数相同,则认为该节点是一个 <strong>好节点</strong>。</p>
20+
21+
<p>返回给定树中<strong> 好节点 </strong>的数量。</p>
22+
23+
<p><strong>子树</strong>&nbsp;指的是一个节点以及它所有后代节点构成的一棵树。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong class="example">示例 1:</strong></p>
30+
31+
<div class="example-block">
32+
<p><strong>输入:</strong><span class="example-io">edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]</span></p>
33+
34+
<p><strong>输出:</strong><span class="example-io">7</span></p>
35+
36+
<p><strong>说明:</strong></p>
37+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3249.Count%20the%20Number%20of%20Good%20Nodes/images/tree1.png" style="width: 360px; height: 158px;" />
38+
<p>树的所有节点都是好节点。</p>
39+
</div>
40+
41+
<p><strong class="example">示例 2:</strong></p>
42+
43+
<div class="example-block">
44+
<p><strong>输入:</strong><span class="example-io">edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]]</span></p>
45+
46+
<p><strong>输出:</strong><span class="example-io">6</span></p>
47+
48+
<p><strong>说明:</strong></p>
49+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3249.Count%20the%20Number%20of%20Good%20Nodes/images/screenshot-2024年06月03日-193552.png" style="width: 360px; height: 303px;" />
50+
<p>树中有 6 个好节点。上图中已将这些节点着色。</p>
51+
</div>
52+
53+
<p><strong class="example">示例 3:</strong></p>
54+
55+
<div class="example-block">
56+
<p><span class="example-io"><b>输入:</b>edges = [[0,1],[1,2],[1,3],[1,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[9,12],[10,11]]</span></p>
57+
58+
<p><span class="example-io"><b>输出:</b>12</span></p>
59+
60+
<p><strong>解释:</strong></p>
61+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3249.Count%20the%20Number%20of%20Good%20Nodes/images/rob.jpg" style="width: 450px; height: 277px;" />
62+
<p>除了节点 9 以外其他所有节点都是好节点。</p>
63+
</div>
64+
65+
<p>&nbsp;</p>
66+
67+
<p><strong>提示:</strong></p>
68+
69+
<ul>
70+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
71+
<li><code>edges.length == n - 1</code></li>
72+
<li><code>edges[i].length == 2</code></li>
73+
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
74+
<li>输入确保 <code>edges</code> 总表示一棵有效的树。</li>
75+
</ul>
76+
77+
<!-- description:end -->
78+
79+
## 解法
80+
81+
<!-- solution:start -->
82+
83+
### 方法一:DFS
84+
85+
我们先根据题目给定的边 $\textit{edges}$ 构建出树的邻接表 $\textit{g},ドル其中 $\textit{g}[a]$ 表示节点 $a$ 的所有邻居节点。
86+
87+
然后,我们设计一个函数 $\textit{dfs}(a, \textit{fa}),ドル表示计算以节点 $a$ 为根的子树中的节点数,并累计好节点的数量。其中 $\textit{fa}$ 表示节点 $a$ 的父节点。
88+
89+
函数 $\textit{dfs}(a, \textit{fa})$ 的执行过程如下:
90+
91+
1. 初始化变量 $\textit{pre} = -1,ドル $\textit{cnt} = 1,ドル $\textit{ok} = 1,ドル分别表示节点 $a$ 的某个子树的节点数、节点 $a$ 的所有子树的节点数、以及节点 $a$ 是否为好节点。
92+
2. 遍历节点 $a$ 的所有邻居节点 $b,ドル如果 $b$ 不等于 $\textit{fa},ドル则递归调用 $\textit{dfs}(b, a),ドル返回值为 $\textit{cur},ドル并累加到 $\textit{cnt}$ 中。如果 $\textit{pre} < 0,ドル则将 $\textit{cur}$ 赋值给 $\textit{pre}$;否则,如果 $\textit{pre}$ 不等于 $\textit{cur},ドル说明节点 $a$ 的不同子树的节点数不同,将 $\textit{ok}$ 置为 0ドル$。
93+
3. 最后,累加 $\textit{ok}$ 到答案中,并返回 $\textit{cnt}$。
94+
95+
在主函数中,我们调用 $\textit{dfs}(0, -1),ドル最后返回答案。
96+
97+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 表示节点的数量。
98+
99+
<!-- tabs:start -->
100+
101+
#### Python3
102+
103+
```python
104+
class Solution:
105+
def countGoodNodes(self, edges: List[List[int]]) -> int:
106+
def dfs(a: int, fa: int) -> int:
107+
pre = -1
108+
cnt = ok = 1
109+
for b in g[a]:
110+
if b != fa:
111+
cur = dfs(b, a)
112+
cnt += cur
113+
if pre < 0:
114+
pre = cur
115+
elif pre != cur:
116+
ok = 0
117+
nonlocal ans
118+
ans += ok
119+
return cnt
120+
121+
g = defaultdict(list)
122+
for a, b in edges:
123+
g[a].append(b)
124+
g[b].append(a)
125+
ans = 0
126+
dfs(0, -1)
127+
return ans
128+
```
129+
130+
#### Java
131+
132+
```java
133+
class Solution {
134+
private int ans;
135+
private List<Integer>[] g;
136+
137+
public int countGoodNodes(int[][] edges) {
138+
int n = edges.length + 1;
139+
g = new List[n];
140+
Arrays.setAll(g, k -> new ArrayList<>());
141+
for (var e : edges) {
142+
int a = e[0], b = e[1];
143+
g[a].add(b);
144+
g[b].add(a);
145+
}
146+
dfs(0, -1);
147+
return ans;
148+
}
149+
150+
private int dfs(int a, int fa) {
151+
int pre = -1, cnt = 1, ok = 1;
152+
for (int b : g[a]) {
153+
if (b != fa) {
154+
int cur = dfs(b, a);
155+
cnt += cur;
156+
if (pre < 0) {
157+
pre = cur;
158+
} else if (pre != cur) {
159+
ok = 0;
160+
}
161+
}
162+
}
163+
ans += ok;
164+
return cnt;
165+
}
166+
}
167+
```
168+
169+
#### C++
170+
171+
```cpp
172+
class Solution {
173+
public:
174+
int countGoodNodes(vector<vector<int>>& edges) {
175+
int n = edges.size() + 1;
176+
vector<int> g[n];
177+
for (const auto& e : edges) {
178+
int a = e[0], b = e[1];
179+
g[a].push_back(b);
180+
g[b].push_back(a);
181+
}
182+
int ans = 0;
183+
auto dfs = [&](auto&& dfs, int a, int fa) -> int {
184+
int pre = -1, cnt = 1, ok = 1;
185+
for (int b : g[a]) {
186+
if (b != fa) {
187+
int cur = dfs(dfs, b, a);
188+
cnt += cur;
189+
if (pre < 0) {
190+
pre = cur;
191+
} else if (pre != cur) {
192+
ok = 0;
193+
}
194+
}
195+
}
196+
ans += ok;
197+
return cnt;
198+
};
199+
dfs(dfs, 0, -1);
200+
return ans;
201+
}
202+
};
203+
```
204+
205+
#### Go
206+
207+
```go
208+
func countGoodNodes(edges [][]int) (ans int) {
209+
n := len(edges) + 1
210+
g := make([][]int, n)
211+
for _, e := range edges {
212+
a, b := e[0], e[1]
213+
g[a] = append(g[a], b)
214+
g[b] = append(g[b], a)
215+
}
216+
var dfs func(int, int) int
217+
dfs = func(a, fa int) int {
218+
pre, cnt, ok := -1, 1, 1
219+
for _, b := range g[a] {
220+
if b != fa {
221+
cur := dfs(b, a)
222+
cnt += cur
223+
if pre < 0 {
224+
pre = cur
225+
} else if pre != cur {
226+
ok = 0
227+
}
228+
}
229+
}
230+
ans += ok
231+
return cnt
232+
}
233+
dfs(0, -1)
234+
return
235+
}
236+
```
237+
238+
#### TypeScript
239+
240+
```ts
241+
function countGoodNodes(edges: number[][]): number {
242+
const n = edges.length + 1;
243+
const g: number[][] = Array.from({ length: n }, () => []);
244+
for (const [a, b] of edges) {
245+
g[a].push(b);
246+
g[b].push(a);
247+
}
248+
let ans = 0;
249+
const dfs = (a: number, fa: number): number => {
250+
let [pre, cnt, ok] = [-1, 1, 1];
251+
for (const b of g[a]) {
252+
if (b !== fa) {
253+
const cur = dfs(b, a);
254+
cnt += cur;
255+
if (pre < 0) {
256+
pre = cur;
257+
} else if (pre !== cur) {
258+
ok = 0;
259+
}
260+
}
261+
}
262+
ans += ok;
263+
return cnt;
264+
};
265+
dfs(0, -1);
266+
return ans;
267+
}
268+
```
269+
270+
<!-- tabs:end -->
271+
272+
<!-- solution:end -->
273+
274+
<!-- problem:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /