Skip to content

Commit dc6f618

Browse files
update 337
1 parent 38abe44 commit dc6f618

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_337.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
import java.util.Map;
77

88
public class _337 {
9-
109
public static class Solution1 {
11-
//simple recursion without cacheing: 1189 ms
10+
//simple recursion without caching: 1189 ms
1211
public int rob(TreeNode root) {
1312
if (root == null) {
1413
return 0;
@@ -27,8 +26,8 @@ public int rob(TreeNode root) {
2726
}
2827

2928
public static class Solution2 {
30-
//same idea, but with cacheing via a hashmap: 8 ms
31-
public int rob_dp(TreeNode root) {
29+
//same idea, but with caching via a hashmap: 8 ms
30+
public int rob(TreeNode root) {
3231
Map<TreeNode, Integer> map = new HashMap<>();
3332
return getMaxValue(root, map);
3433
}

Diff for: src/test/java/com/fishercoder/_337Test.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.TreeUtils;
4+
import com.fishercoder.solutions._337;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class _337Test {
13+
private static _337.Solution1 solution1;
14+
private static _337.Solution2 solution2;
15+
16+
@BeforeEach
17+
public void setup() {
18+
solution1 = new _337.Solution1();
19+
solution2 = new _337.Solution2();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
assertEquals(7, solution1.rob(TreeUtils.constructBinaryTree(Arrays.asList(3, 2, 3, null, 3, null, 1))));
25+
assertEquals(7, solution2.rob(TreeUtils.constructBinaryTree(Arrays.asList(3, 2, 3, null, 3, null, 1))));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)