Skip to content

Commit 9376adc

Browse files
authored
Merge pull request #619 from LetMeFly666/3249
添加问题“3249.统计好节点的数目”的代码和题解
2 parents 8472d91 + 043fd35 commit 9376adc

10 files changed

+428
-51
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-14 12:49:44
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-14 12:55:36
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
int ans;
14+
vector<vector<int>> graph;
15+
16+
int dfs(int root, int parent=-1) {
17+
int cnt = 1;
18+
int oneChild = 0;
19+
bool thisNodeOk = true;
20+
for (int nextNode : graph[root]) {
21+
if (nextNode == parent) {
22+
continue;
23+
}
24+
int nextCnt = dfs(nextNode, root);
25+
cnt += nextCnt;
26+
if (oneChild && nextCnt != oneChild) {
27+
thisNodeOk = false;
28+
}
29+
oneChild = nextCnt;
30+
}
31+
if (thisNodeOk) {
32+
ans++;
33+
}
34+
return cnt;
35+
}
36+
public:
37+
int countGoodNodes(vector<vector<int>>& edges) {
38+
ans = 0;
39+
graph.resize(edges.size() + 1);
40+
for (vector<int>& edge : edges) {
41+
graph[edge[0]].push_back(edge[1]);
42+
graph[edge[1]].push_back(edge[0]);
43+
}
44+
dfs(0);
45+
return ans;
46+
}
47+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-14 13:15:02
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-14 13:24:43
6+
*/
7+
package main
8+
9+
// import "fmt"
10+
11+
func countGoodNodes(edges [][]int) (ans int) {
12+
graph := make([][]int, len(edges) + 1)
13+
for _, edge := range edges {
14+
graph[edge[0]] = append(graph[edge[0]], edge[1])
15+
graph[edge[1]] = append(graph[edge[1]], edge[0])
16+
}
17+
var dfs func(int, int) int
18+
dfs = func(thisNode, lastNode int) int {
19+
// fmt.Println(thisNode, lastNode)
20+
cnt, oneChild, ok := 1, 0, true
21+
for _, nextNode := range graph[thisNode] {
22+
if nextNode == lastNode {
23+
continue
24+
}
25+
thisChild := dfs(nextNode, thisNode)
26+
cnt += thisChild
27+
if oneChild == 0 {
28+
oneChild = thisChild
29+
} else if oneChild != thisChild {
30+
ok = false
31+
}
32+
}
33+
if ok {
34+
ans++
35+
}
36+
return cnt
37+
}
38+
dfs(0, -1)
39+
return ans
40+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-14 13:00:47
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-14 13:14:26
6+
*/
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
class Solution {
12+
private List<Integer>[] graph;
13+
private int ans;
14+
15+
private int dfs(int thisNode, int lastNode) {
16+
int cnt = 1;
17+
int oneChild = 0;
18+
boolean ok = true;
19+
for (int nextChilld : graph[thisNode]) {
20+
if (nextChilld == lastNode) {
21+
continue;
22+
}
23+
int thisChild = dfs(nextChilld, thisNode);
24+
cnt += thisChild;
25+
if (oneChild == 0) {
26+
oneChild = thisChild;
27+
} else if (oneChild != thisChild) {
28+
ok = false;
29+
}
30+
}
31+
if (ok) {
32+
ans++;
33+
}
34+
return cnt;
35+
}
36+
37+
public int countGoodNodes(int[][] edges) {
38+
ans = 0;
39+
graph = new ArrayList[edges.length + 1];
40+
Arrays.setAll(graph, i -> new ArrayList<>());
41+
for (int[] edge : edges) {
42+
graph[edge[0]].add(edge[1]);
43+
graph[edge[1]].add(edge[0]);
44+
}
45+
dfs(0, -1);
46+
return ans;
47+
}
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-14 12:56:25
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-14 13:00:23
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def dfs(self, thisNode: int, parentNode: int=-1) -> int:
11+
cnt, oneChild, ok = 1, 0, True
12+
for nextNode in self.graph[thisNode]:
13+
if nextNode == parentNode:
14+
continue
15+
nextChild = self.dfs(nextNode, thisNode)
16+
cnt += nextChild
17+
if not oneChild:
18+
oneChild = nextChild
19+
elif oneChild != nextChild:
20+
ok = False
21+
if ok:
22+
self.ans += 1
23+
return cnt
24+
25+
def countGoodNodes(self, edges: List[List[int]]) -> int:
26+
self.graph = [[] for _ in range(len(edges) + 1)]
27+
for x, y in edges:
28+
self.graph[x].append(y)
29+
self.graph[y].append(x)
30+
self.ans = 0
31+
self.dfs(0)
32+
return self.ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@
719719
|3222.求出硬币游戏的赢家|简单|<a href="https://leetcode.cn/problems/find-the-winning-player-in-coin-game/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/05/LeetCode%203222.%E6%B1%82%E5%87%BA%E7%A1%AC%E5%B8%81%E6%B8%B8%E6%88%8F%E7%9A%84%E8%B5%A2%E5%AE%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143501415" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-winning-player-in-coin-game/solutions/2977629/letmefly-3222qiu-chu-ying-bi-you-xi-de-y-08j3/" target="_blank">LeetCode题解</a>|
720720
|3226.使两个整数相等的位更改次数|简单|<a href="https://leetcode.cn/problems/number-of-bit-changes-to-make-two-integers-equal/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/02/LeetCode%203226.%E4%BD%BF%E4%B8%A4%E4%B8%AA%E6%95%B4%E6%95%B0%E7%9B%B8%E7%AD%89%E7%9A%84%E4%BD%8D%E6%9B%B4%E6%94%B9%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143448117" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-bit-changes-to-make-two-integers-equal/solutions/2974558/letmefly-3226shi-liang-ge-zheng-shu-xian-j01h/" target="_blank">LeetCode题解</a>|
721721
|3242.设计相邻元素求和服务|简单|<a href="https://leetcode.cn/problems/design-neighbor-sum-service/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/11/LeetCode%203242.%E8%AE%BE%E8%AE%A1%E7%9B%B8%E9%82%BB%E5%85%83%E7%B4%A0%E6%B1%82%E5%92%8C%E6%9C%8D%E5%8A%A1/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143698347" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/design-neighbor-sum-service/solutions/2985408/letmefly-3242she-ji-xiang-lin-yuan-su-qi-mc7m/" target="_blank">LeetCode题解</a>|
722+
|3249.统计好节点的数目|中等|<a href="https://leetcode.cn/problems/count-the-number-of-good-nodes/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/14/LeetCode%203249.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E8%8A%82%E7%82%B9%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143768804" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-good-nodes/solutions/2988295/letmefly-3249tong-ji-hao-jie-dian-de-shu-zojm/" target="_blank">LeetCode题解</a>|
722723
|3254.长度为K的子数组的能量值I|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/06/LeetCode%203254.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143575677" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-i/solutions/2979562/letmefly-3254chang-du-wei-k-de-zi-shu-zu-iu8u/" target="_blank">LeetCode题解</a>|
723724
|3255.长度为K的子数组的能量值II|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/07/LeetCode%203255.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143591327" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/2980432/letmefly-3255chang-du-wei-k-de-zi-shu-zu-rags/" target="_blank">LeetCode题解</a>|
724725
|3258.统计满足K约束的子字符串数量I|简单|<a href="https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/12/LeetCode%203258.%E7%BB%9F%E8%AE%A1%E6%BB%A1%E8%B6%B3K%E7%BA%A6%E6%9D%9F%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E9%87%8FI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143726399" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/solutions/2986598/letmefly-3258tong-ji-man-zu-k-yue-shu-de-fs44/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)