Created
October 24, 2014 23:09
-
-
Save anonymous/a11ec3734335f4b8b557 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/ruby | |
def bottom_up(p, n) | |
r = Array.new(n,0) | |
(1..n).each do |j| | |
q = -1 | |
(1..j).each do |i| | |
q = [q, r[j-1] + p[i]].max | |
end | |
r[j] = q | |
end | |
return r[n]; | |
end | |
profit = {0=>0,1=>1,2=>5,3=>4,4=>8} | |
puts bottom_up(profit,3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment