- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of coins needed to make the changes in Python

Suppose we have coins of different denominations (1,5,10,25) 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. So if the input is 64, the output is 7. This is formed using 25 + 25 + 10 + 1 + 1 + 1 + 1 = 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, amount): coins = [1,5,10,25] 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(64))

## Input

64

## Output

7

- Related Questions & Answers
- Program to find number of coins needed to make the changes with given set of coins in Python
- C++ program to count number of minimum coins needed to get sum k
- C++ program to count number of operations needed to reach n by paying coins
- Program to find number of combinations of coins to reach target in Python
- Program to find number of distinct coin sums we can make with coins and quantities in Python?
- Program to check minimum number of characters needed to make string palindrome in Python
- Program to find maximum number of coins we can collect in Python
- C++ program to find minimum how many coins needed to buy binary string
- C++ program to find minimum how many operations needed to make number 0
- Program to find maximum number of coins we can get using Python
- C++ program to find minimum number of punches are needed to make way to reach target
- Program to find minimum number of rocketships needed for rescue in Python
- C++ program to count minimum number of operations needed to make number n to 1
- Program to find minimum operations needed to make two arrays sum equal in Python
- Program to count number of operations needed to make string as concatenation of same string twice in Python

Advertisements