Find Minimum Number Of Coins
Dora, the explorer, visits India and decides to try the famous Indian food. However, the restaurant accepts only Indian currency i.e. [1, 2, 5, 10, 20, 50, 100, 500, 1000] valued coins.
So, Dora goes to a bank that has an infinite supply of each of the denominations to make a change for a given ‘Amount’ of money. As a cashier at the bank, your task is to provide Dora the minimum number of coins that add up to the given ‘Amount’.
For Example
For Amount = 70, the minimum number of coins required is 2 i.e an Rs. 50 coin and a Rs. 20 coin.
Note
It is always possible to find the minimum number of coins for the given amount. So, the answer will always exist.
Input Format
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.
The only line of each test case contains a single integer ‘Amount’ representing the amount Dora wishes to change to Indian currency.
Output Format
For each test case, print the minimum number of coins needed to make the change.
Print the output of each test case in a separate line.
Note
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints
1 <= T <= 100
1 <= Amount <= 10^5
Where 'T' is the number of test-cases
Time Limit: 1sec
We can use dynamic programming to solve this problem.
We maintain a vector ‘dp’ of size AMOUNT+1 and initialize it to a maximum value (say AMOUNT + 1). Here, dp[i] stores the minimum number of coins needed to change an amount ‘i’.
Algorithm
- We set dp[0] to 0 as minimum coins to change an amount of ‘0’ is 0.
- We then traverse denominations from beginning to end (loop variable i).
- We loop from denominations[i] to amount (loop variable j).
- Now, if we pick the i-th coin, we will be interested in minimum coins needed to make a change of amount j - denominations[i].
- So, we set dp[j] to the minimum of dp[j] and dp[j - denominations[i]] + 1.
- We loop from denominations[i] to amount (loop variable j).
- Finally, we return dp[amount] as answer.
We will use the greedy approach to solve our problem. The intuition behind this idea is that since we need to minimize the total number of coins needed to make the change, taking coins with large denominations (i.e. the best available option) in each iteration can yield the best overall result.
Algorithm
- We will first sort the ‘denominations’ array, if not sorted.
- We initialize ‘coinsCount’ to 0 and traverse ‘denominations’ from the end to the beginning.
- While denominations[i] is greater than or equal to ‘amount’-
- We increment ‘coinsCount’ by one.
- We subtract denominations[i] from ‘amount’.
- If ‘amount’ becomes 0, it means we have found a solution. So, we simply break from the loop.
- While denominations[i] is greater than or equal to ‘amount’-
- Finally, we return ‘coinsCount’ as our result.
We know that if we have an amount of X and a certain type of coin, say Y, then we can make a change of int(X/Y) coins and an amount of X%Y will remain unchanged. So, we can use this idea to further optimize our previous approach in which we were counting a coin again and again.
Algorithm
- We will first sort the ‘denominations’ array/list, if not sorted.
- We initialize ‘coinsCount’ to 0 and traverse ‘denominations’ from the end to the beginning.
- Increment ‘coinsCount’ by int(AMOUNT / denominations[i]).
- Set ‘AMOUNT’ to AMOUNT % denominations[i].
- Finally, we return ‘coinsCount’ as our result.