6
6
import java .util .List ;
7
7
import java .util .Map ;
8
8
9
- /**
10
- * 377. Combination Sum IV
11
- *
12
- * Given an integer array with all positive numbers and no duplicates,
13
- * find the number of possible combinations that add up to a positive integer target.
14
-
15
- Example:
16
-
17
- nums = [1, 2, 3]
18
- target = 4
19
-
20
- The possible combination ways are:
21
- (1, 1, 1, 1)
22
- (1, 1, 2)
23
- (1, 2, 1)
24
- (1, 3)
25
- (2, 1, 1)
26
- (2, 2)
27
- (3, 1)
28
-
29
- Note that different sequences are counted as different combinations.
30
-
31
- Therefore the output is 7.
32
-
33
- Follow up:
34
- What if negative numbers are allowed in the given array?
35
- How does it change the problem?
36
- What limitation we need to add to the question to allow negative numbers?
37
- */
38
-
39
9
public class _377 {
40
10
41
11
public static class Solution1 {
@@ -94,16 +64,16 @@ public static class Solution3 {
94
64
/**
95
65
* Time: O(n^2)
96
66
* Space: O(n)
97
- *
67
+ * <p>
98
68
* Since this question doesn't require to return all the combination result, instead, it just wants one number, we could use DP
99
69
* the idea is similar to Climbing Stairs.
100
- *
70
+ * <p>
101
71
* The idea is very clear as the code speaks for itself:
102
72
* It's easy to find the routine
103
73
* dp[0] = 0;
104
74
* dp[1] = 1;
105
75
* ...
106
- *
76
+ * <p>
107
77
* Reference: https://discuss.leetcode.com/topic/52186/my-3ms-java-dp-solution
108
78
*/
109
79
public int combinationSum4 (int [] nums , int target ) {
@@ -128,7 +98,7 @@ public static class Solution4 {
128
98
/**
129
99
* Time: O(n)
130
100
* Space: O(n)
131
- *
101
+ * <p>
132
102
* Reference: https://discuss.leetcode.com/topic/52255/java-recursion-solution-using-hashmap-as-memory
133
103
*/
134
104
public static Map <Integer , Integer > map = new HashMap <>();//need to remove public static before submitting on Leetcode as it doesn't reset static variables
0 commit comments