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 allConstruct = (target, wordBank) => { | |
| const table = new Array(target.length + 1) | |
| .fill().map(() => []); | |
| table[0] = [[]]; | |
| for (let i = 0; i <= target.length; i += 1) { | |
| const oldCombinations = table[i]; | |
| if (oldCombinations.length === 0) continue; | |
| const rest = target.slice(i); | |
| for (let word of wordBank) { | |
| if (!rest.startsWith(word)) continue; |
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 countConstruct = (target, wordBank) => { | |
| const table = new Array(target.length + 1).fill(0); | |
| table[0] = 1; | |
| for (let i = 0; i <= target.length; i += 1) { | |
| if (!table[i]) continue; | |
| const rest = target.slice(i); | |
| for (let word of wordBank) { | |
| if (!rest.startsWith(word)) continue; | |
| const spacesAhead = word.length; | |
| table[i + spacesAhead] += table[i]; |
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 canConstruct = (target, wordBank) => { | |
| const table = new Array(target.length + 1).fill(false); | |
| // for each index of table, looking at that index | |
| // will be looking at the substring up to but not including that value | |
| // 0 would be true | |
| table[0] = true; | |
| for (let i = 0; i <= target.length; i += 1) { | |
| if (!table[i]) continue; | |
| const rest = target.slice(i); | |
| for (let word of wordBank) { |
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 bestSum = (goal, nums) => { | |
| const table = new Array(goal + 1).fill(null); | |
| table[0] = []; | |
| // want to update table only if our new array is better | |
| for (let i = 0; i <= goal; i += 1) { | |
| const currentArr = table[i]; | |
| if (!currentArr) continue; | |
| for (let currentNum of nums) { | |
| const newSum = i + currentNum; | |
| if (newSum > goal) continue; |
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 = (goal, nums) => { | |
| const table = new Array(goal + 1).fill(null); | |
| table[0] = []; | |
| // as long as there is a way to make table[i] (null check) | |
| // there will be a way to make table[i] + num | |
| // check bounds | |
| for (let i = 0; i <= goal; i += 1) { | |
| const currentArr = table[i]; | |
| if (currentArr) { | |
| const sum = currentArr.reduce(((cur, total) => cur + total), 0); |
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 canSum = (goal, nums) => { | |
| const table = new Array(goal + 1).fill(false); | |
| table[0] = true; | |
| for (let i = 0; i <= goal; i += 1) { | |
| // i is current value | |
| // if i is true, then i + nums[j] is also true | |
| if (table[i]) { | |
| for (let j = 0; j < nums.length; j += 1) { | |
| const currentVal = i + nums[j]; | |
| if (currentVal <= goal) table[currentVal] = true; |
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 gridTraveler = (m, n) => { | |
| // create 2 dimensional array | |
| const grid = new Array(m + 1) | |
| .fill().map(() => new Array(n + 1).fill(0)); | |
| grid[1][1] = 1; | |
| for (let i = 0; i < m + 1; i += 1) { | |
| for (let j = 0; j < n + 1; j += 1) { | |
| // add current value to right and down if possible | |
| const currentValue = grid[i][j]; |
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 allConstruct = (target, wordBank, memo = new Map()) => { | |
| if (memo.has(target)) return memo.get(target); | |
| if (target.length === 0) return [[]]; | |
| const res = []; | |
| for (let word of wordBank) { | |
| if (!target.startsWith(word)) continue; | |
| const remainder = target.slice(word.length); | |
| const partialArr = allConstruct(remainder, wordBank, memo).map((arr) => [word, ...arr]); | |
| res.push(...partialArr); | |
| } |
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 countConstruct = (target, wordbank, memo = new Map()) => { | |
| if (memo.has(target)) return memo.get(target); | |
| if (target.length === 0) return 0; | |
| let ways = 0; | |
| for(let word of wordbank) { | |
| if (!target.startsWith(word)) continue; | |
| const remainder = target.slice(word.length); | |
| if (remainder.length === 0) ways += 1; | |
| const waysRecursive = countConstruct(remainder, wordbank, memo); | |
| ways += waysRecursive; |
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 bestSum = (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; | |
| let smallestSum = null; | |
| for (let num of numbers) { | |
| const remainder = targetSum - num; | |
| const res = bestSum(remainder, numbers, memo); | |
| if (res === null) continue; |