-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanSum.js
60 lines (49 loc) · 1.69 KB
/
canSum.js
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
//Combinatoric Problem
//m = targetSum
//n = number.length
//Brute Force:
//time: O(n^m)
//space: O(m)
//Memoized version:
//time: O(n*m)
//space: O(m)
//write a fn canSum(targetSum, numberArray) that takes in a targetSum and an array of numbers and returns a boolean indicating whether or not it is possible to create the targetSum using any combination of the number any number of times. All inputs are assumed to be non-negative
function canSum(targetSum, numbers) {
if (targetSum === 0) return true;
if (targetSum < 0) return false;
for (let num of numbers) {
const remainder = targetSum - num;
const nextTargetResult = canSum(remainder, numbers);
if (nextTargetResult === true) {
return true;
}
}
return false;
}
console.log(canSum(7, [2,3])) //true;
console.log(canSum(7, [5,3,4,7])) //true;
console.log(canSum(7, [2,4])) //false;
console.log(canSum(8, [2,3,5])) //true;
// console.log(canSum(300, [7,14])) //false;
function canSumMemo(targetSum, numbers, memo={}) {
if (targetSum in memo) return memo[targetSum];
if (targetSum === 0) return true;
if (targetSum < 0) return false;
for (let num of numbers) {
const remainder = targetSum - num;
if (canSumMemo(remainder, numbers, memo) === true) {
memo[remainder] = true;
return true;
}
}
memo[targetSum] = false;
return false;
}
console.log(canSumMemo(7, [2,3])) //true;
console.log(canSumMemo(7, [5,3,4,7])) //true;
console.log(canSumMemo(7, [2,4])) //false;
console.log(canSumMemo(8, [2,3,5])) //true;
console.log(canSumMemo(300, [7,14])) //false;
for (let num in {1: 'one', 2: 'two'}) {
console.log(num);
}