Created
March 28, 2019 00:41
-
-
Save spiterman/cf1fc72fbfe2c1bb4665471bf8533a80 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
| 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