Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7bf365e

Browse files
add 1273
1 parent a421220 commit 7bf365e

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ _If you like this project, please leave me a star._ ★
4444
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | |Easy||
4545
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java) | |Medium||
4646
|1275|[Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1275.java) | |Easy|Array|
47+
|1273|[Delete Tree Nodes](https://leetcode.com/problems/delete-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1273.java) | |Medium|Dynamic Programming, DFS |
4748
|1271|[Hexspeak](https://leetcode.com/problems/hexspeak/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1271.java) | |Easy||
4849
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1267.java) | |Medium||
4950
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | |Easy||
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1273. Delete Tree Nodes
7+
*
8+
* A tree rooted at node 0 is given as follows:
9+
* The number of nodes is nodes;
10+
* The value of the i-th node is value[i];
11+
* The parent of the i-th node is parent[i].
12+
* Remove every subtree whose sum of values of nodes is zero.
13+
* After doing so, return the number of nodes remaining in the tree.
14+
*
15+
* Example 1:
16+
* 0 (1)
17+
* / \
18+
* (-2) 1 2 (4)
19+
* / / | \
20+
* (0)3 (-2)4 (-1)5 6(-1)
21+
*
22+
* Input: nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1]
23+
* Output: 2
24+
*
25+
* Constraints:
26+
* 1 <= nodes <= 10^4
27+
* -10^5 <= value[i] <= 10^5
28+
* parent.length == nodes
29+
* parent[0] == -1 which indicates that 0 is the root.
30+
* */
31+
public class _1273 {
32+
public static class Solution1 {
33+
public int deleteTreeNodes(int nodes, int[] parent, int[] value) {
34+
int len = parent.length;
35+
for (int i = len - 1; i > 0; ) {
36+
int sum = 0;
37+
int parentIndex = parent[i];
38+
while (i > 0 && parent[i] == parentIndex) {
39+
sum += value[i--];
40+
}
41+
value[parentIndex] = value[parentIndex] + sum;
42+
}
43+
for (int i = 0; i < value.length; i++) {
44+
if (value[i] == 0) {
45+
for (int j = 0; j < parent.length; j++) {
46+
if (parent[j] == i) {
47+
value[j] = 0;
48+
}
49+
}
50+
}
51+
}
52+
return (int) Arrays.stream(value).filter(i -> i != 0).count();
53+
}
54+
}
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1273;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1273Test {
10+
private static _1273.Solution1 solution1;
11+
private static int[] parent;
12+
private static int[] value;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1273.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
parent = new int[]{-1, 0, 0, 1, 2, 2, 2};
22+
value = new int[]{1, -2, 4, 0, -2, -1, -1};
23+
assertEquals(2, solution1.deleteTreeNodes(7, parent, value));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
parent = new int[]{-1, 0, 0, 1, 2, 2, 2};
29+
value = new int[]{1, -2, 3, 0, -2, -1, 0};
30+
assertEquals(2, solution1.deleteTreeNodes(7, parent, value));
31+
}
32+
33+
@Test
34+
public void test3() {
35+
parent = new int[]{-1, 0, 0, 1, 2, 2, 2};
36+
value = new int[]{1, -2, 4, 0, -2, -1, -2};
37+
assertEquals(6, solution1.deleteTreeNodes(7, parent, value));
38+
}
39+
40+
@Test
41+
public void test4() {
42+
parent = new int[]{-1, 0, 0, 1, 2, 2, 2};
43+
value = new int[]{3, -2, 4, 0, -2, -1, -2};
44+
assertEquals(0, solution1.deleteTreeNodes(7, parent, value));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)