Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2014 23:09
Show Gist options
  • Save anonymous/a11ec3734335f4b8b557 to your computer and use it in GitHub Desktop.
Save anonymous/a11ec3734335f4b8b557 to your computer and use it in GitHub Desktop.
#!/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