You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.You may assume that you have an infinite number of each kind of coin. Example 1:Input: coins = [1,2,5], amount = 11Output: 3Explanation: 11 = 5 + 5 + 1Example 2:Input: coins = [2], amount = 3Output: -1Example 3:Input: coins = [1], amount = 0Output: 0 Constraints:1 <= coins.length <= 121 <= coins[i] <= 231 - 10 <= amount <= 104
Question
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.You may assume that you have an infinite number of each kind of coin. Example 1:Input: coins = [1,2,5], amount = 11Output: 3Explanation: 11 = 5 + 5 + 1Example 2:Input: coins = [2], amount = 3Output: -1Example 3:Input: coins = [1], amount = 0Output: 0 Constraints:1 <= coins.length <= 121 <= coins[i] <= 231 - 10 <= amount <= 104
Solution
To solve this problem, we can use a dynamic programming approach. We will create an array dp of size amount + 1, where dp[i] represents the fewest number of coins needed to make up the amount i.
First, we initialize all elements of dp to infinity, except for dp[0] which is set to 0, since we don't need any coins to make up an amount of 0.
Then, for each coin in the coins array, we iterate through the dp array from the value of the coin up to the amount. For each value j, we update dp[j] to be the minimum of dp[j] and dp[j - coin] + 1. This means that we are considering using the current coin to make up the amount j, and we take the minimum of the previous number of coins needed for amount j and the number of coins needed for amount j - coin plus one more coin.
Finally, if dp[amount] is still infinity, it means that it is not possible to make up the amount using the given coins, so we return -1. Otherwise, we return dp[amount] as the fewest number of coins needed.
Here is the implementation in Python:
def coinChange(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0
for coin in coins:
for j in range(coin, amount + 1):
dp[j] = min(dp[j], dp[j - coin] + 1)
if dp[amount] == float('inf'):
return -1
else:
return dp[amount]
coins = [1, 2, 5] amount = 11 print(coinChange(coins, amount)) # Output: 3
coins = [2] amount = 3 print(coinChange(coins, amount)) # Output: -1
coins = [1] amount = 0 print(coinChange(coins, amount)) # Output: 0
The time complexity of this solution is O(amount * n), where n is the number of coins, and the space complexity is O(amount).
Similar Questions
You are given an integer array coins representing coins of different denominations and an integer k.You have an infinite number of coins of each denomination. However, you are not allowed to combine coins of different denominations.Return the kth smallest amount that can be made using these coins. Example 1:Input: coins = [3,6,9], k = 3Output: 9Explanation: The given coins can make the following amounts:Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.All of the coins combined produce: 3, 6, 9, 12, 15, etc.Example 2:Input: coins = [5,2], k = 7Output: 12 Explanation: The given coins can make the following amounts:Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.All of the coins combined produce: 2, 4, 5, 6, 8, 10, 12, 14, 15, etc. Constraints:1 <= coins.length <= 151 <= coins[i] <= 251 <= k <= 2 * 109coins contains pairwise distinct integers.Java 1class Solution {2 public long findKthSmallest(int[] coins, int k) {3 4 }5} Custom TestcaseUse Example Testcases Run Code SubmitCopyright © 2024 LeetCodeHelp CenterJobsBug BountyOnline InterviewStudentsTermsPrivacy PolicyUnited States
ou are given an array of integers ‘coins’ denoting the denomination of coins and another array of integers ‘freq’ denoting the number of coins of each denomination.You have to find the number of ways to make the sum ‘V’ by selecting some(or all) coins from the array.The answer can be very large. So, return the answer modulo 1000000007.For Example :‘N’ = 3, ‘coins’ = {1, 2, 3}, ‘freq’ = {1, 1, 3}, ‘V’ = 6For the given example, we can make six by using the following coins:{1, 2, 3}{3. 3}Hence, the answer is 2.
Write a program that prints the minimum number of coins to make change for an amount of money.Usage: ./change centswhere cents is the amount of cents you need to give backif the number of arguments passed to your program is not exactly 1, print Error, followed by a new line, and return 1you should use atoi to parse the parameter passed to your programIf the number passed as the argument is negative, print 0, followed by a new lineYou can use an unlimited number of coins of values 25, 10, 5, 2, and 1 cent
Pranav is playing a coin game with his friends. Create a simple program to assist Pranav in converting a given amount of cash into the minimum number of coins. Prompt Pranav to input the cash amount and utilize assignment operators to calculate and display the minimum number of coins needed for denominations of 100, 50, 10, 5, 2, and 1. Assume these denominations as coins with values of 100, 50, 10, 5, 2, and 1, respectively
Single File Programming QuestionProblem StatementPranav is playing a coin game with his friends. Create a simple program to assist Pranav in converting a given amount of cash into the minimum number of coins. Prompt Pranav to input the cash amount and utilize assignment operators to calculate and display the minimum number of coins needed for denominations of 100, 50, 10, 5, 2, and 1. Assume these denominations as coins with values of 100, 50, 10, 5, 2, and 1, respectively.Note:Give the input greater than 100.Input format :The input is a single integer c representing the amount of cash.Output format :The output displays the minimum number of rupee notes for denominations of 100, 50, 10, 5, 2, and 1.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:100 ≤ c ≤ 10000Do not use any looping structures.Sample test cases :Input 1 :100Output 1 :1Input 2 :589Output 2 :12Input 3 :10000Output 3 :100Code Size : 1024 kbNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.