Karthik Selvakumar Bhuvaneswaran is a Software Engineer working on Saas(RoR) and PaaS(Salesforce.com) applications. He beleives on day starts at night and requires nothing more than Music, a cup of cofee and a fully charged laptop.
© 2016 karthikselva. Distributed with an MIT license.
class Rod attr_accessor :price_list, :cache def initialize(price_list) @price_list = price_list @cache = {} end def cut(n) return 0 if n < 1 val = [] n.times do |i| i = i+1 if @cache[n-i].nil? val << cut(n-i) + @price_list[i] else val << @price_list[i] + @cache[n-i] end end @cache[n] = val.max return @cache[n] end end rd = Rod.new({ 1 => 1, 2 => 5, 3 => 8, 4 => 9, 5 => 10, 6 => 17, 7 => 17, 8 => 20 }) p rd.cut(5) # Output: 13 # => 2 -> 5, 3 -> 8 = 8 + 5 => 13