Skip to content

Commit 2ee8b9e

Browse files
add 3004
1 parent d27a6cd commit 2ee8b9e

File tree

3 files changed

+128
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+128
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@
4545
| 3014 | [Minimum Number of Pushes to Type Word I](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3014.java) | | Easy |
4646
| 3006 | [Find Beautiful Indices in the Given Array I](https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3006.java) | | Medium |
4747
| 3005 | [Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3005.java) | | Easy |
48+
| 3004 | [Maximum Subtree of the Same Color](https://leetcode.com/problems/maximum-subtree-of-the-same-color/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3004.java) | | Medium | DFS, Tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 _3004 {
9+
public static class Solution1 {
10+
/**
11+
* My completely original solution.
12+
* Practice makes perfect!
13+
* Post-order traversal is the way to go since we need to process all children first before processing any particular node.
14+
*/
15+
class ColoredTreeNode {
16+
int val;
17+
int color;
18+
List<ColoredTreeNode> children;
19+
boolean allSubtreeSameColor;
20+
int totalChildrenCount;
21+
22+
public ColoredTreeNode(int val, int color) {
23+
this.val = val;
24+
this.color = color;
25+
this.children = new ArrayList<>();
26+
this.allSubtreeSameColor = true;//initialize to be true until it's built/proven to be false
27+
this.totalChildrenCount = 1;//count itself as its own child
28+
}
29+
}
30+
31+
int maxSize = 0;
32+
33+
public int maximumSubtreeSize(int[][] edges, int[] colors) {
34+
if (edges == null || edges.length == 0 || edges[0].length == 0) {
35+
return colors.length > 0 ? 1 : 0;
36+
}
37+
ColoredTreeNode root = buildTree(edges, colors);
38+
int totalNodeCount = postOrder(root);
39+
if (root.allSubtreeSameColor) {
40+
return totalNodeCount;
41+
}
42+
return maxSize;
43+
}
44+
45+
private int postOrder(ColoredTreeNode root) {
46+
if (root == null) {
47+
return 0;
48+
}
49+
int totalChildrenCount = 1;//count itself as a child
50+
for (ColoredTreeNode child : root.children) {
51+
int count = postOrder(child);
52+
totalChildrenCount += count;
53+
if (root.color != child.color || !child.allSubtreeSameColor) {
54+
root.allSubtreeSameColor = false;
55+
}
56+
}
57+
root.totalChildrenCount = totalChildrenCount;
58+
if (root.allSubtreeSameColor) {
59+
maxSize = Math.max(maxSize, root.totalChildrenCount);
60+
}
61+
return totalChildrenCount;
62+
}
63+
64+
private ColoredTreeNode buildTree(int[][] edges, int[] colors) {
65+
Map<Integer, ColoredTreeNode> map = new HashMap<>();
66+
for (int i = 0; i < edges.length; i++) {
67+
ColoredTreeNode parent = map.getOrDefault(edges[i][0], new ColoredTreeNode(edges[i][0], colors[edges[i][0]]));
68+
ColoredTreeNode child = map.getOrDefault(edges[i][1], new ColoredTreeNode(edges[i][1], colors[edges[i][1]]));
69+
parent.children.add(child);
70+
map.put(edges[i][0], parent);
71+
map.put(edges[i][1], child);
72+
}
73+
return map.get(0);
74+
}
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3004;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3004Test {
10+
private static _3004.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3004.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.maximumSubtreeSize(new int[][]{
20+
{0, 1}, {0, 2}, {0, 3}
21+
}, new int[]{1, 1, 2, 3}));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(4, solution1.maximumSubtreeSize(new int[][]{
27+
{0, 1}, {0, 2}, {0, 3}
28+
}, new int[]{1, 1, 1, 1}));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(3, solution1.maximumSubtreeSize(new int[][]{
34+
{0, 1}, {0, 2}, {2, 3}, {2, 4}
35+
}, new int[]{1, 2, 3, 3, 3}));
36+
}
37+
38+
@Test
39+
public void test4() {
40+
assertEquals(1, solution1.maximumSubtreeSize(new int[][]{
41+
{}
42+
}, new int[]{1}));
43+
}
44+
45+
@Test
46+
public void test5() {
47+
assertEquals(1, solution1.maximumSubtreeSize(new int[][]{
48+
{0, 1}, {1, 2}
49+
}, new int[]{1, 1, 2}));
50+
}
51+
}

0 commit comments

Comments
 (0)