Skip to content

Commit 8a6c4d1

Browse files
add 3249
1 parent 9db165d commit 8a6c4d1

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+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class _3249 {
9+
public static class Solution1 {
10+
/**
11+
* My completely original solution during the contest.
12+
*/
13+
class TreeNode {
14+
int val;
15+
List<TreeNode> children;
16+
int totalChildrenCount;
17+
18+
public TreeNode(int val) {
19+
this.val = val;
20+
this.children = new ArrayList<>();
21+
this.totalChildrenCount = 1;//count itself as its own child
22+
}
23+
}
24+
25+
int goodNodes = 0;
26+
27+
public int countGoodNodes(int[][] edges) {
28+
if (edges == null || edges.length == 0 || edges[0].length == 0) {
29+
return 0;
30+
}
31+
TreeNode root = buildTree(edges);
32+
postOrder(root);
33+
dfs(root);
34+
return goodNodes;
35+
}
36+
37+
private void dfs(TreeNode root) {
38+
if (root == null || root.children.isEmpty()) {
39+
return;
40+
}
41+
int size = root.children.get(0).totalChildrenCount;
42+
if (size == 0) {
43+
return;
44+
}
45+
boolean possiblyGoodNode = true;
46+
for (TreeNode child : root.children) {
47+
if (child.totalChildrenCount != size) {
48+
possiblyGoodNode = false;
49+
break;
50+
}
51+
}
52+
if (possiblyGoodNode) {
53+
goodNodes++;
54+
}
55+
for (TreeNode child : root.children) {
56+
dfs(child);
57+
}
58+
}
59+
60+
private int postOrder(TreeNode root) {
61+
if (root == null) {
62+
return 0;
63+
}
64+
if (root.children.isEmpty()) {
65+
goodNodes++;
66+
return 1;
67+
}
68+
int totalChildrenCount = 1;
69+
for (TreeNode child : root.children) {
70+
int count = postOrder(child);
71+
totalChildrenCount += count;
72+
}
73+
root.totalChildrenCount = totalChildrenCount;
74+
return totalChildrenCount;
75+
}
76+
77+
private TreeNode buildTree(int[][] edges) {
78+
Map<Integer, TreeNode> map = new HashMap<>();
79+
for (int i = 0; i < edges.length; i++) {
80+
if (edges[i][0] == 0 || edges[i][1] == 0) {
81+
if (edges[i][0] == 0) {
82+
TreeNode parent = map.getOrDefault(edges[i][0], new TreeNode(edges[i][0]));
83+
TreeNode child = map.getOrDefault(edges[i][1], new TreeNode(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+
TreeNode parent = map.getOrDefault(edges[i][1], new TreeNode(edges[i][1]));
89+
TreeNode child = map.getOrDefault(edges[i][0], new TreeNode(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+
TreeNode parent = map.get(edges[i][0]);
97+
TreeNode child = map.getOrDefault(edges[i][1], new TreeNode(edges[i][1]));
98+
parent.children.add(child);
99+
map.put(edges[i][0], parent);
100+
map.put(edges[i][1], child);
101+
} else if (map.containsKey(edges[i][1])) {
102+
TreeNode parent = map.get(edges[i][1]);
103+
TreeNode child = map.getOrDefault(edges[i][0], new TreeNode(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+
return map.get(0);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy