-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminimum_number_of_coins.rb
More file actions
35 lines (29 loc) · 886 Bytes
/
Copy pathminimum_number_of_coins.rb
File metadata and controls
35 lines (29 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Given a value V, if we want to make a change for V Rs, and we have an infinite supply of each of the denominations in Indian currency,
# i.e., we have an infinite supply of { 1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes,
# what is the minimum number of coins and/or notes needed to make the change?
# Input: V = 70
# Output: 2
# We need a 50 Rs note and a 20 Rs note.
#
# Input: V = 121
# Output: 3
# We need a 100 Rs note, a 20 Rs note and a 1 Rs coin.
def coin_change(coins, amount)
return 0 if amount.zero?
output = 0
remaining = amount
coins.sort! { |a, b| b <=> a }
coins.each do |coin|
num = remaining / coin
next if num.zero?
remaining -= (coin * num)
output += num
return output if remaining.zero?
end
-1
end
# coins = [1, 2, 5]
deno = [1, 2, 5, 10, 20, 50,
100, 500, 1000]
amount = 11
puts coin_change(deno, amount)