Movatterモバイル変換


[0]ホーム

URL:


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

Commit8a6c4d1

Browse files
add 3249
1 parent9db165d commit8a6c4d1

File tree

2 files changed

+114
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src/main/java/com/fishercoder/solutions/fourththousand

2 files changed

+114
-0
lines changed

‎paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3249|[Count the Number of Good Nodes](https://leetcode.com/problems/count-the-number-of-good-nodes/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3249.java)|| Medium|
34
| 3248|[Snake in Matrix](https://leetcode.com/problems/snake-in-matrix/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3248.java)|| Easy|
45
| 3243|[Shortest Distance After Road Addition Queries I](https://leetcode.com/problems/shortest-distance-after-road-addition-queries-i/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3243.java)|| Medium|
56
| 3242|[Design Neighbor Sum Service](https://leetcode.com/problems/design-neighbor-sum-service/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3242.java)|| Easy|
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
packagecom.fishercoder.solutions.fourththousand;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.HashMap;
5+
importjava.util.List;
6+
importjava.util.Map;
7+
8+
publicclass_3249 {
9+
publicstaticclassSolution1 {
10+
/**
11+
* My completely original solution during the contest.
12+
*/
13+
classTreeNode {
14+
intval;
15+
List<TreeNode>children;
16+
inttotalChildrenCount;
17+
18+
publicTreeNode(intval) {
19+
this.val =val;
20+
this.children =newArrayList<>();
21+
this.totalChildrenCount =1;//count itself as its own child
22+
}
23+
}
24+
25+
intgoodNodes =0;
26+
27+
publicintcountGoodNodes(int[][]edges) {
28+
if (edges ==null ||edges.length ==0 ||edges[0].length ==0) {
29+
return0;
30+
}
31+
TreeNoderoot =buildTree(edges);
32+
postOrder(root);
33+
dfs(root);
34+
returngoodNodes;
35+
}
36+
37+
privatevoiddfs(TreeNoderoot) {
38+
if (root ==null ||root.children.isEmpty()) {
39+
return;
40+
}
41+
intsize =root.children.get(0).totalChildrenCount;
42+
if (size ==0) {
43+
return;
44+
}
45+
booleanpossiblyGoodNode =true;
46+
for (TreeNodechild :root.children) {
47+
if (child.totalChildrenCount !=size) {
48+
possiblyGoodNode =false;
49+
break;
50+
}
51+
}
52+
if (possiblyGoodNode) {
53+
goodNodes++;
54+
}
55+
for (TreeNodechild :root.children) {
56+
dfs(child);
57+
}
58+
}
59+
60+
privateintpostOrder(TreeNoderoot) {
61+
if (root ==null) {
62+
return0;
63+
}
64+
if (root.children.isEmpty()) {
65+
goodNodes++;
66+
return1;
67+
}
68+
inttotalChildrenCount =1;
69+
for (TreeNodechild :root.children) {
70+
intcount =postOrder(child);
71+
totalChildrenCount +=count;
72+
}
73+
root.totalChildrenCount =totalChildrenCount;
74+
returntotalChildrenCount;
75+
}
76+
77+
privateTreeNodebuildTree(int[][]edges) {
78+
Map<Integer,TreeNode>map =newHashMap<>();
79+
for (inti =0;i <edges.length;i++) {
80+
if (edges[i][0] ==0 ||edges[i][1] ==0) {
81+
if (edges[i][0] ==0) {
82+
TreeNodeparent =map.getOrDefault(edges[i][0],newTreeNode(edges[i][0]));
83+
TreeNodechild =map.getOrDefault(edges[i][1],newTreeNode(edges[i][1]));
84+
parent.children.add(child);
85+
map.put(edges[i][0],parent);
86+
map.put(edges[i][1],child);
87+
}else {
88+
TreeNodeparent =map.getOrDefault(edges[i][1],newTreeNode(edges[i][1]));
89+
TreeNodechild =map.getOrDefault(edges[i][0],newTreeNode(edges[i][0]));
90+
parent.children.add(child);
91+
map.put(edges[i][1],parent);
92+
map.put(edges[i][0],child);
93+
}
94+
}else {
95+
if (map.containsKey(edges[i][0])) {
96+
TreeNodeparent =map.get(edges[i][0]);
97+
TreeNodechild =map.getOrDefault(edges[i][1],newTreeNode(edges[i][1]));
98+
parent.children.add(child);
99+
map.put(edges[i][0],parent);
100+
map.put(edges[i][1],child);
101+
}elseif (map.containsKey(edges[i][1])) {
102+
TreeNodeparent =map.get(edges[i][1]);
103+
TreeNodechild =map.getOrDefault(edges[i][0],newTreeNode(edges[i][0]));
104+
parent.children.add(child);
105+
map.put(edges[i][1],parent);
106+
map.put(edges[i][0],child);
107+
}
108+
}
109+
}
110+
returnmap.get(0);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp