File tree 1 file changed +68
-0
lines changed
1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ \\TLE DFS
2
+ class Solution {
3
+ public int combinationSum4(int[] nums, int target) {
4
+ if(target==0){
5
+ return 1;
6
+ }
7
+ int res = 0;
8
+ for(int i:nums){
9
+ if(i<=target){
10
+ res+=combinationSum4(nums,target - i);
11
+ }
12
+ }
13
+ return res;
14
+ }
15
+ }
16
+
17
+ \\Top-down
18
+ class Solution {
19
+
20
+ int[] dp ;
21
+ public int combinationSum4(int[] nums, int target) {
22
+
23
+ dp=new int[target+1];
24
+ Arrays.fill(dp,-1);
25
+
26
+ dp[0] =1;
27
+
28
+ helper(nums,target);
29
+ return dp[target];
30
+ }
31
+
32
+ int helper(int[] nums,int target){
33
+
34
+ if(dp[target]>-1){
35
+ return dp[target];
36
+ }
37
+ int res = 0;
38
+ for(int i:nums){
39
+ if(i<=target){
40
+ res+=helper(nums,target - i);
41
+ }
42
+ }
43
+ dp[target]=res;
44
+ return dp[target];
45
+ }
46
+ }
47
+
48
+ \\Bottom-up
49
+ class Solution {
50
+
51
+ int[] dp ;
52
+ public int combinationSum4(int[] nums, int target) {
53
+
54
+ dp=new int[target+1];
55
+
56
+ dp[0] =1;
57
+
58
+ for(int i = 1 ; i<=target ;i++){
59
+ for(int n:nums){
60
+ if(i>=n){
61
+ dp[i] += dp[i-n];
62
+ }
63
+ }
64
+ }
65
+
66
+ return dp[target];
67
+ }
68
+ }
You can’t perform that action at this time.
0 commit comments