Consider a cashier machine that takes the amount of your itemspayments in coins. We feed the machine coins one by one until the value is more than the amount we should have payedpaid. Then the machine returns the extra amount in coins in the least number of coins possible. So we have k
type of coins and a limited amount of each type. Also the machine has an unlimited amount of each of the k
typetypes of coins so it can returnsreturn us the exact amount it should ( therethere is always coin with value 1
). So we want to minimize the number of coins we have after we have payedpaid for the items we have bought. considerConsider this example: We have 3
kinds of coins of values 1
, 2
and 4
and we have to pay 15
dollars. We have 10
coins with value 1
, 7
coins with value 4
and 5
coins with value 2
. There are multiple ways to minimize our coins after the process. One of them is to give the machine 10 coins with value 1 and 3 coins with value 2. Thus we have payedpaid 16 dollars with 13 coins and the machine should return to us 1 dollar and it returns us 1 coin with value 1. So we have spent 12 coins. The other way is to give the machine 9 coins with value 1 and 3 coins with value 2 and the machine doesn't return us anything. So we have payedpaid 12 coins.
My approach to the problem is to divide the problem to two separate coin change problem. The easier problem is when the machine returns us the extra amount of money we have payedpaid. I think this is a simple coin change problem that we want to change some amount of money to the minimum number of coins. The main problem is to pay the machine some maximal amount of coin with the restrictions mentioned above. The restrictions were that we have limited number of each coin and if want to pay A
dollars the machine stops receiving coins when the amount of money we have payedpaid becomes equal or greater than A
. So For example if we have to pay to 15 dollars and we want to use a total amount of 18 dollars to pay we should use at least a coin with value 4 (18-15+1) or else at some point in the process of paying the amount we have given to the machine surpasses 15 and it won't receive anymoreany more coins.