Skip to content

Instantly share code, notes, and snippets.

@spiterman
Created March 28, 2019 00:41
Show Gist options
  • Select an option

  • Save spiterman/cf1fc72fbfe2c1bb4665471bf8533a80 to your computer and use it in GitHub Desktop.

Select an option

Save spiterman/cf1fc72fbfe2c1bb4665471bf8533a80 to your computer and use it in GitHub Desktop.
let counter = 0;
function houseRobberRecursive(arr) {
function stealFromHouse(index) {
if(index >= arr.length) {
counter++;
return 0;
}
return Math.max(arr[index] + stealFromHouse(index + 2), stealFromHouse(index + 1));
}
return stealFromHouse(0);
}
// console.log(houseRobberRecursive([3, 1, 2, 5, 4, 2, 5]))
// houseRobberRecursive([1, 2, 3, 4])
// console.log(counter)
let results = {};
let array = [];
for(let i = 0; i <= 20; i++) {
houseRobberRecursive(array);
results[i] = counter;
counter = 0;
array.push(i);
}
console.log(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment