Created
November 19, 2013 08:01
-
-
Save avanishgiri/7541885 to your computer and use it in GitHub Desktop.
dynamic programming solution to robbers problem -- runs in linear time in the size of the array. tested against my previous brute force solution.
This file contains 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
a = [447, 462, 9, 272, 242, 0, 206, 998, 35, 414] | |
def max(a, i, memo = {}) | |
return 0 if i == 0 | |
return a[i] if i == 1 | |
return memo[i] if memo[i] | |
return memo[i] = [max(a,i-2) + a[i], max(a,i-1)].max | |
end | |
p max(a, 9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment