-
Notifications
You must be signed in to change notification settings - Fork 5
/
CombinationSum4.java
66 lines (61 loc) · 1.69 KB
/
CombinationSum4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*https://leetcode.com/problems/combination-sum-iv/*/
class Solution
{
private int[] dp;
public int combinationSum4(int[] nums, int target)
{
dp = new int[target+1];
Arrays.fill(dp, -1);
dp[0] = 1;
return combination(nums, target);
}
private int combination(int[] arr, int n) {
if(dp[n] != -1) {
return dp[n];
}
int count = 0;
for (int i = 0; i < arr.length; ++i)
{
if(n >= arr[i])
count += combination(arr, n-arr[i]);
}
dp[n] = count;
return count;
}
}
class Solution {
Integer[] table;
public int combinationSum4(int[] nums, int target) {
table = new Integer[target+1];
return countCombinations(nums,target,0);
}
private int countCombinations(int[] nums, int target, int sum)
{
if (sum == target) return 1;
if (table[sum] != null) return table[sum];
int count = 0;
for (int i = 0; i < nums.length; ++i)
if (sum+nums[i] <= target)
count += countCombinations(nums,target,sum+nums[i]);
table[sum] = count;
return count;
}
}
class Solution {
Integer[] table;
public int combinationSum4(int[] nums, int target) {
table = new Integer[target+1];
return countCombinations(nums,target);
}
private int countCombinations(int[] nums, int target)
{
if (target == 0) return 1;
if (table[target] != null) return table[target];
int count = 0;
for (int i = 0; i < nums.length; ++i)
if (nums[i] <= target)
count += countCombinations(nums,target-nums[i]);
table[target] = count;
return count;
}
}