mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-25 21:58:57 +01:00
Fix syntax issues
This commit is contained in:
parent
1553e26a91
commit
8363ae81fd
3 changed files with 28 additions and 29 deletions
|
@ -1,30 +1,31 @@
|
|||
def coin_change_minimum(coins, amount)
|
||||
dp = Array.new(amount+1,-1)
|
||||
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
|
||||
if dp[i - coin] != -1
|
||||
dp[i] = -1 == dp[i] ? dp[i - coin] + 1 : [dp[i], dp[i - coin] + 1].min
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
dp[amount]
|
||||
end
|
||||
end
|
||||
|
||||
def coin_change_combinations(coins, amount)
|
||||
dp = Array.new(coins.length+1) { Array.new(amount+1,0) }
|
||||
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]])
|
||||
dp[i][j] = dp[i - 1][j] + (j < coins[i - 1] ? 0 : dp[i][j - coins[i - 1]])
|
||||
end
|
||||
end
|
||||
dp[coins.length][amount]
|
||||
end
|
||||
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 + "."
|
||||
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 + '.'
|
||||
|
|
|
@ -34,9 +34,7 @@ def decimal_to_binary(d)
|
|||
return binary if d == 0
|
||||
return 1.to_s if d == 1
|
||||
|
||||
binary = decimal_to_binary(d / 2).to_s + binary
|
||||
|
||||
binary
|
||||
decimal_to_binary(d / 2).to_s + binary
|
||||
end
|
||||
|
||||
puts 'Binary value of 4 is ' + decimal_to_binary(4).to_s
|
||||
|
|
Loading…
Reference in a new issue