Ninja went to the toffee shop, and he purchased some toffies worth 'V' cents. Ninja has an unlimited supply of coins of 1, 2, 5, 10, 20, 50, 100, 500, and 1000 cents. Now Ninja wants to know the minimum number of coins he needs to pay to the shopkeeper.
Your task is to find the minimum number of coins Ninja needs to pay to the shopkeeper so as to pay 'V' cents to him.
Note: You have to solve this problem using the greedy approach.
Example:Input: 'V' = 60
Output: 2
Ninja need to pay two coins only 50 + 10 = 60
The first line of input contains an integer 'T', denoting the number of test cases.
Each test case will contain only one integer 'V' on a separate line.
Output format :
For each test case, print the minimum number of coins Ninja needs to pay to the shopkeeper.
Output for each test case will be printed in a separate line.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= 'V' <= 10^5
Time Limit: 1sec
2
70
50
2
1
For the first test case, Ninja need to pay two coins, only 50 + 20 = 70
For the second test case, Ninja needs to pay only one coin of 50 cents.
2
121
100
3
1
Can we start distributing coins greedily by greater value?
Approach: We will first take coins with a greater value for this problem. This will reduce the total number of coins. We will start from the largest possible coin and keep adding coins till the remaining value is not zero.
Algorithm :
O(V), Where 'V' is the given input integer.
As we are iterating till the ‘V’ is zero hence the time complexity will be O(V)
O(1)
As we are using the constant extra space, the space complexity will be O(1)