programming-examples/ruby/Algorithms/min_change_sum.rb

18 lines
417 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
# write a function to determine the minimum number of coins used that sum to a value
# ex coins = [1, 3, 5] total = 11
def min_change_sum(coins, total)
totals = Array.new(total + 1, 1_000_000_000_000)
totals[0] = 0
(1..total).each do |i|
coins.each do |coin|
if coin <= i && (totals[i - coin] + 1 < totals[i])
totals[i] = totals[i - coin] + 1
end
end
end
totals[total]
end