TheAlgorithms-Ruby/dynamic_programming/coin_change.rb

32 lines
982 B
Ruby
Raw Normal View History

def coin_change_minimum(coins, amount)
2021-03-10 04:03:25 +01:00
dp = Array.new(amount + 1, -1)
dp[0] = 0
coins.each do |coin|
(coin..amount).each do |i|
if dp[i - coin] != -1
dp[i] = -1 == dp[i] ? dp[i - coin] + 1 : [dp[i], dp[i - coin] + 1].min
2021-02-28 13:08:27 +01:00
end
end
end
2021-03-10 04:03:25 +01:00
dp[amount]
end
def coin_change_combinations(coins, amount)
dp = Array.new(coins.length + 1) { Array.new(amount + 1, 0) }
dp[0][0] = 1
(1..coins.length).each do |i|
(0..amount).each do |j|
dp[i][j] = dp[i - 1][j] + (j < coins[i - 1] ? 0 : dp[i][j - coins[i - 1]])
2021-02-28 13:08:27 +01:00
end
end
2021-03-10 04:03:25 +01:00
dp[coins.length][amount]
end
coins = Array[2, 4, 5]
amount = 12
puts 'Number of combinations of getting change for ' + amount.to_s + ' is ' + coin_change_combinations(coins,
amount).to_s + '.'
puts 'Minimum number of coins required for ' + amount.to_s + ' is ' + coin_change_minimum(coins, amount).to_s + '.'