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

Commit 63c6c59

Browse files
refactor 377
1 parent 5e78f73 commit 63c6c59

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/main/java/com/fishercoder/solutions/_377.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.HashMap;
56
import java.util.List;
7+
import java.util.Map;
68

79
/**
810
* 377. Combination Sum IV
@@ -67,6 +69,7 @@ public static class Solution2 {
6769
* However, it also ended up in TLE by this testcase: [1,2,3], 32
6870
*/
6971
public static int count = 0;
72+
7073
public int combinationSum4(int[] nums, int target) {
7174
Arrays.sort(nums);
7275
backtracking(nums, target, new ArrayList());
@@ -88,6 +91,9 @@ private void backtracking(int[] nums, int target, List<Integer> list) {
8891

8992
public static class Solution3 {
9093
/**
94+
* Time: O(n^2)
95+
* Space: O(n)
96+
*
9197
* Since this question doesn't require to return all the combination result, instead, it just wants one number, we could use DP
9298
* the idea is similar to Climbing Stairs.
9399
*
@@ -116,4 +122,32 @@ public int combinationSum4(int[] nums, int target) {
116122
return result[target];
117123
}
118124
}
125+
126+
public static class Solution4 {
127+
/**
128+
* Time: O(n)
129+
* Space: O(n)
130+
*
131+
* Reference: https://discuss.leetcode.com/topic/52255/java-recursion-solution-using-hashmap-as-memory
132+
*/
133+
public static Map<Integer, Integer> map = new HashMap<>();//need to remove public static before submitting on Leetcode as it doesn't reset static variables
134+
135+
public int combinationSum4(int[] nums, int target) {
136+
if (nums == null || nums.length == 0 || target < 0) {
137+
return 0;
138+
}
139+
if (target == 0) {
140+
return 1;
141+
}
142+
if (map.containsKey(target)) {
143+
return map.get(target);
144+
}
145+
int count = 0;
146+
for (int num : nums) {
147+
count += combinationSum4(nums, target - num);
148+
}
149+
map.put(target, count);
150+
return count;
151+
}
152+
}
119153
}

src/test/java/com/fishercoder/_377Test.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._377;
4+
import org.junit.Before;
45
import org.junit.BeforeClass;
56
import org.junit.Test;
67

@@ -10,6 +11,7 @@ public class _377Test {
1011
private static _377.Solution1 solution1;
1112
private static _377.Solution2 solution2;
1213
private static _377.Solution3 solution3;
14+
private static _377.Solution4 solution4;
1315
private static int[] nums;
1416
private static int target;
1517

@@ -18,6 +20,14 @@ public static void setup() {
1820
solution1 = new _377.Solution1();
1921
solution2 = new _377.Solution2();
2022
solution3 = new _377.Solution3();
23+
solution4 = new _377.Solution4();
24+
}
25+
26+
@Before
27+
public void setUp() throws Exception {
28+
//always have to reset these global variables before using it again
29+
solution2.count = 0;
30+
solution4.map.clear();
2131
}
2232

2333
@Test
@@ -27,6 +37,7 @@ public void test1() {
2737
assertEquals(7, solution1.combinationSum4(nums, target));
2838
assertEquals(7, solution2.combinationSum4(nums, target));
2939
assertEquals(7, solution3.combinationSum4(nums, target));
40+
assertEquals(7, solution4.combinationSum4(nums, target));
3041
}
3142

3243
@Test
@@ -35,10 +46,21 @@ public void test2() {
3546
target = 32;
3647
// assertEquals(39882198, solution1.combinationSum4(nums, target));//this results in MLE, so comment out
3748

38-
solution2.count = 0;//always have to reset this global variable to be zero before using it again
3949
assertEquals(39882198, solution2.combinationSum4(nums, target));
4050

4151
assertEquals(39882198, solution3.combinationSum4(nums, target));
52+
53+
assertEquals(39882198, solution4.combinationSum4(nums, target));
54+
}
55+
56+
@Test
57+
public void test3() {
58+
nums = new int[]{9};
59+
target = 3;
60+
assertEquals(0, solution1.combinationSum4(nums, target));
61+
assertEquals(0, solution2.combinationSum4(nums, target));
62+
assertEquals(0, solution3.combinationSum4(nums, target));
63+
assertEquals(0, solution4.combinationSum4(nums, target));
4264
}
4365

4466
}

0 commit comments

Comments
 (0)