Skip to content

Instantly share code, notes, and snippets.

@joneshf
Forked from anonymous/gist:a11ec3734335f4b8b557
Last active August 29, 2015 14:08
Show Gist options
  • Save joneshf/e81e7394034d7ae6fe10 to your computer and use it in GitHub Desktop.
Save joneshf/e81e7394034d7ae6fe10 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-i] + 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