Skip to content

Commit 4c6685f

Browse files
add 1343
1 parent f327c43 commit 4c6685f

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1343|[Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium||
1112
|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1333.java) | |Medium||
1213
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
1314
|1329|[Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1329.java) | |Medium||
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class _1343 {
9+
public static class Solution1 {
10+
public int maxProduct(TreeNode root) {
11+
Set<Long> set = new HashSet<>();
12+
int total = dfs(root, set);
13+
long result = 0l;
14+
for (long sum : set) {
15+
result = Math.max(result, sum * (total - sum));
16+
}
17+
return (int) (result % 1000000007);
18+
}
19+
20+
private int dfs(TreeNode root, Set<Long> set) {
21+
if (root == null) {
22+
return 0;
23+
}
24+
root.val += dfs(root.left, set);
25+
root.val += dfs(root.right, set);
26+
set.add((long) root.val);
27+
return root.val;
28+
}
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1343;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _1343Test {
14+
private static _1343.Solution1 solution1;
15+
private static TreeNode root;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1343.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6));
25+
assertEquals(110, solution1.maxProduct(root));
26+
}
27+
28+
}

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