Created
          December 5, 2020 02:44 
        
      - 
      
- 
        Save iJustErikk/3e8814deeab970200bb898451eb5a957 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const howSum = (targetSum, numbers, memo = new Map()) => { | |
| const key = `${targetSum}`; | |
| if (memo.has(key)) return memo.get(key); | |
| if (targetSum === 0) return []; | |
| if (targetSum <= 0) return null; | |
| for (let num of numbers) { | |
| const remainder = targetSum - num; | |
| const res = howSum(remainder, numbers, memo); | |
| if (res === null) continue; | |
| res.push(num); | |
| memo.set(key, res); | |
| return memo.get(key); | |
| } | |
| memo.set(key, null); | |
| return null; | |
| }; | |
| console.log(howSum(7, [3,4])); | |
| console.log(howSum(7, [3, 4, 5, 7])); //returns 3,4 or 7 | |
| console.log(howSum(7, [2, 4]));// returns null | |
| console.log(howSum(300, [7, 14])); | |
| console.log(howSum(301, [1, 2, 3])); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment