-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestSum.js
More file actions
68 lines (64 loc) · 2.08 KB
/
BestSum.js
File metadata and controls
68 lines (64 loc) · 2.08 KB
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
67
68
//m= target sum
//n= array length
//Recursion
/*const bestSum =(targetsum, numbers) => { // T.C. = O(n^m*m) S.C. = O(m^2)
if(targetsum===0) return [];
if(targetsum<0) return null;
let remainder;
let ret;
let best=null;
for(let num of numbers) {
//console.log(num);
remainder=targetsum-num;
ret=bestSum(remainder, numbers)
if(ret!=null) {
ret=[...ret,num];
// if the combination is shortest than the current best or not yet any then update
if(best===null||ret.length<best.length)
best=ret;
}
}
return best;
}*/
//Memoization
/*const bestSum =(targetsum, numbers,memo = {}) => { // T.C. = O(n*m^2) S.C. = O(m^2)
if(targetsum in memo) return memo[targetsum];
if(targetsum===0) return [];
if(targetsum<0) return null;
let remainder;
let ret;
let best=null;
for(let num of numbers) {
//console.log(num);
remainder=targetsum-num;
ret=bestSum(remainder, numbers,memo)
if(ret!=null) {
ret=[...ret,num];
// if the combination is shortest than the current best or not yet any then update
if(best===null||ret.length<best.length)
best=ret;
}
}
memo[targetsum]= best;
return best;
}*/
//Tabulation
const bestSum =(targetsum,numbers) => { // T.C. = O(n*m^2) S.C. = O(m^2)
const table=Array(targetsum+1).fill(null);
table[0]=[];
for(let i=0;i<=table.length;i++) {
if(table[i]!=null) {
for(let num of numbers) {
if(i+num<=targetsum&&table[i+num]===null) // table[i+num]===null is same as table[i+num].length>table[i].length+1
table[i+num]=[num, ...table[i]];
}
}
}
return table[targetsum];
}
console.log(bestSum(7,[2,3]));//[ 3, 2, 2 ]
console.log(bestSum(7,[2,4]));//null
console.log(bestSum(7,[2,5,3,7]));//[ 7 ]
console.log(bestSum(8,[3,2,5]));//[ 5, 3 ]
console.log(bestSum(300,[7,14,51,1]));//[ 1, 1, 1, 51, 51, 51, 51, 51, 14, 14, 14 ]
console.log(bestSum(300,[7,14,57]))//null