You are viewing a single comment's thread. Return to all comments →
here is my solution PLUS Fix for broken input for multiple input test cases.
function unboundedKnapsack(k, arr, memo=new Array(k+1).fill(null)) { // Write your code here if(memo[k]!==null) return memo[k]; let result=0; for (let i=0;i<arr.length;i++){ let cur=arr[i]; if (cur>k) continue; result=Math.max(result,cur + unboundedKnapsack(k-cur,arr,memo)); } memo[k]=result; return result; } function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH); const t = parseInt(readLine().trim(), 10); for(let i=0;i<t;i++){ var firstMultipleInput = readLine().replace(/\s+$/g, '').split(' '); var n = parseInt(firstMultipleInput[0], 10); var k = parseInt(firstMultipleInput[1], 10); var arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10)); var result = unboundedKnapsack(k, arr); ws.write(result + '\n'); } ws.end(); }
Seems like cookies are disabled on this browser, please enable them to open this website
Knapsack
You are viewing a single comment's thread. Return to all comments →
here is my solution PLUS Fix for broken input for multiple input test cases.