
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find number of coins needed to make the changes with given set of coins in Python
Suppose we have coins of different denominations and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. When that amount of money cannot be accommodated by any combination of the coins, return -1. So if the input is [1,2,5], and the amount is 64, the output is 14. This is formed using 12*5 + 2 + 2 = 64.
To solve this, we will follow these steps −
- if amount = 0, then return 0
- if minimum of coins array > amount, then return -1
- define one array called dp, of size amount + 1, and fill this with -1
- for i in range coins array
- if i > length of dp – 1, then skip the next part, go for the next iteration
- dp[i] := 1
- for j in range i + 1 to amount
- if dp[j – 1] = -1, then skip the next part, go for the next iteration
- otherwise if dp[j] = -1, then dp[j] := dp[j - i] + 1
- otherwise dp[j] := minimum of dp[j] and dp[j – i] + 1
- return dp[amount]
Let us see the following implementation to get better understanding −
Example
class Solution(object): def coinChange(self, coins, amount): if amount == 0 : return 0 if min(coins) > amount: return -1 dp = [-1 for i in range(0, amount + 1)] for i in coins: if i > len(dp) - 1: continue dp[i] = 1 for j in range(i + 1, amount + 1): if dp[j - i] == -1: continue elif dp[j] == -1: dp[j] = dp[j - i] + 1 else: dp[j] = min(dp[j], dp[j - i] + 1) return dp[amount] ob1 = Solution() print(ob1.coinChange([1,2,5],64))
Input
[1,2,5], 64
Output
14
- Related Articles
- Program to find number of coins needed to make the changes in Python
- Program to find number of distinct coin sums we can make with coins and quantities in Python?
- C++ program to count number of minimum coins needed to get sum k
- Program to find number of combinations of coins to reach target in Python
- Python program to find ways to get n rupees with given coins
- Minimum number of coins that make a given value
- Program to find maximum number of coins we can collect in Python
- C++ program to count number of operations needed to reach n by paying coins
- Program to find maximum number of coins we can get using Python
- Program to find maximum coins we can get from disappearing coins matrix in Python
- C++ program to find minimum how many coins needed to buy binary string
- I have a total of rupees 300 in coins of denomination rupees 1, 2, 5 the number of rupees 2 coins is 3 times the number of rupees 5 coins. The total number of coins is 160. Find the number of coins of each denomination.
- Program to find the count of coins of each type from the given ratio in C++
- C/C++ Program for Greedy Algorithm to find Minimum number of Coins
- Program to count number of ways we can distribute coins to workers in Python

Advertisements