最小硬币问题_进行更改的最小硬币数量
最小硬幣問題
Description:
描述:
This is classic dynamic programming problem to find minimum number of coins to make a change. This problem has been featured in interview rounds of Amazon, Morgan Stanley, Paytm, Samsung etc.
這是經典的動態編程問題,用于尋找進行更改的最小硬幣數量。 亞馬遜,摩根士丹利,Paytm,三星等公司的采訪回合都突出了這個問題。
Problem statement:
問題陳述:
Given a value P, you have to make change for P cents, given that you have infinite supply of each of C { C1, C2, ... ,Cn} valued coins. Find the minimum number of coins to make the change. Return -1 if the change is not possible with the coins provided.
給定一個值P ,由于您擁有C {C 1 ,C 2 ,...,C n }個有價硬幣的無限供應,因此您必須為P美分進行更改。 找到進行更改的最小硬幣數量 。 如果提供的硬幣無法找零,請返回-1 。
Input:Amount P=13Coin values are:1, 4, 5 Output:3Explanation with example
舉例說明
Let's solve the above example. Total amount is 13 and we have coins with values 1, 4 and 5.
讓我們解決以上示例。 總金額為13,我們有價值分別為1、4和5的硬幣。
Since, we are to find minimum number of coins and we have infinite number of coin for any denomination, it's apparent that we would go for greedy. We should pick the coin with maximum denomination and keep trying with that until the remaining amount of the change is less the denomination of the coin. Then on course we keep choosing the next best one. So, the algorithm would be like.
因為,我們要找到最小數量的硬幣,而對于任何面額我們都擁有無限數量的硬幣,所以顯然我們會貪婪。 我們應該選擇面額最大的硬幣,并繼續嘗試直到零錢的剩余金額少于硬幣面額為止。 然后,當然我們會繼續選擇次佳的。 因此,該算法將是這樣。
1) Let, count=0 to count minimum number of coin used2) Pick up coin with maximum denomination say, value x3) while amount≥xamount=amount-xcount=count+1 4) if amount=0Go to Step 75) Pick up the next best denomination of coin and assign it to x6) Go to Step 27) End. count is the minimum number of coin used.Let's see whether the above greedy algorithm leads to the desired result or not.
讓我們看看上面的貪婪算法是否導致了期望的結果。
So, we would pick up 5 first from {1, 4, 5}We would pick 5 two timesNow amount=3, count=2Next best coin is 4 but 4 > amountNext best coin is 1We would pick 1 three timesAmount=0 and count=5So, minimum number of coins needed for the change is 5.But is it really minimum?If we choose one coin with denomination 5 and two coins with denomination 4, it would make the change and coins needed here is (2+1) = 3 So, greedy does not work as the local optimum choices doesn't make global optimum choice. Hence, we need dynamic programming.Problem Solution Approach
問題解決方法
We have amount M and n number of coins, {C1, C2, ..., Cn}
我們有M個和n個硬幣, {C 1 ,C 2 ,...,C n }
Now,
現在,
We have two choice for any Cj,
對于任何C j ,我們都有兩種選擇。
Use Cj and recur for amount M-Cj
使用C j并遞歸MC j
Don't use Cj and recur for amount M with other coins
不要使用C j并與其他硬幣重現金額M
(m)= minimum number of coins needed to change amount m
(m)=找零數量m所需的最小硬幣數量
We can formulate the above recursion using DP.
我們可以使用DP來制定上述遞歸。
// (for any amount 0 to M, minimum number of coins needed is initially infinity) 1) Initialize DP[M+1] with INT_MAX. 2) DP[0]=0 //base case of above recursion3) for i=1 to M //iterate amountsfor any coin C_jIf i>=C_j && DP[i-C_j]≠INT_MAX && DP[i-C_j]+1<DP[i])DP[i]=DP[i-C_j]+1; //update valueEnd forEnd for The result is DP[M]C++ implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;int coin_change(vector<int> a, int m, int n) {//recursive implementation//// if(m<0)// return;// if(m==0){// if(count<min_count)// min_count=count;// return;// }// for(int i=0;i<n;i++){// coin_change(a,i,m-a[i],n,count+1);// coin_change(a,i+1,m-a[i],n,count+1);// }///DP implementation/////base valueint DP[m + 1];//initializefor (int i = 1; i <= m; i++)DP[i] = INT_MAX;DP[0] = 0;for (int i = 1; i <= m; i++) {for (int j = 0; j < n; j++) {// if amount > coin valueif (i >= a[j] && DP[i - a[j]] != INT_MAX && DP[i - a[j]] + 1 < DP[i])// if updation possible update for minimum valueDP[i] = DP[i - a[j]] + 1;}}return DP[m]; } int main() {int n, item, m;cout << "Enter amount to be changes:\n";cin >> m;cout << "Enter number of coins:\n";cin >> n;cout << "Enter coin values\n";vector<int> a;for (int j = 0; j < n; j++) {scanf("%d", &item);a.push_back(item);}int ans = coin_change(a, m, n);if (ans == INT_MAX)cout << "Coin change not possible" << endl;elsecout << "Minimum number of coins needed: " << ans << endl;return 0; }Output
輸出量
RUN 1: Enter amount to be changes: 13 Enter number of coins: 3 Enter coin values 1 4 5 Minimum number of coins needed: 3RUN 2: Enter amount to be changes: 11 Enter number of coins: 2 Enter coin values 4 5 Coin change not possible翻譯自: https://www.includehelp.com/icp/minimum-number-of-coins-to-make-the-change.aspx
最小硬幣問題
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的最小硬币问题_进行更改的最小硬币数量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单链表遍历_单链表及其遍历实现的基本操作
- 下一篇: php 单例模式有什么缺点_PHP的完整