最小硬币问题

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=13
Coin values are:
1, 4, 5
Output:
3

Explanation 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 used
2) Pick up coin with maximum denomination say, value x
3) while amount≥x
amount=amount-x
count=count+1
4) if amount=0
Go to Step 7
5) Pick up the next best denomination of coin and assign it to x
6) Go to Step 2
7) 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 times
Now amount=3, count=2
Next best coin is 4 but 4 > amount
Next best coin is 1
We would pick 1 three times
Amount=0 and count=5
So, 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 ,我们都有两种选择。

  1. Use Cj and recur for amount M-Cj

    使用C j并递归MC j

  2. 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 recursion
3)  for  i=1 to M //iterate amounts
for any coin C_j
If i>=C_j  && DP[i-C_j]≠INT_MAX && DP[i-C_j]+1<DP[i])
DP[i]=DP[i-C_j]+1; //update value
End for
End 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 value
int DP[m + 1];
//initialize
for (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 value
if (i >= a[j] && DP[i - a[j]] != INT_MAX && DP[i - a[j]] + 1 < DP[i])
// if updation possible update for minimum value
DP[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;
else
cout << "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: 3
RUN 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

最小硬币问题

最小硬币问题_进行更改的最小硬币数量相关推荐

  1. 数组排序最小复杂度_进行排序的最小缺失数

    数组排序最小复杂度 Problem statement: 问题陈述: Given an array of n integers. Find the minimum number of elements ...

  2. mysql最小费用最大流问题_图论-网络流之最小费用最大流问题

    n=5;C=[0 15 16 0 0 0 0 0 13 14 0 11 0 17 0 0 0 0 0 8 0 0 0 0 0]; %弧容量 b=[0 4 1 0 0 0 0 0 6 1 0 2 0 3 ...

  3. mysql最小费用最大流问题_算法笔记_140:最小费用最大流问题(Java)

    packagecom.liuzhen.practice;importjava.util.ArrayList;importjava.util.Scanner;public classMain {publ ...

  4. python 最小硬币数_最小假币问题的Python实现

    假设由29枚硬币,其中一枚假币,假币比真币重,有一个量程和精度足够的天平,怎样保证最少称重次数找出假币? 思路: 将硬币分为3堆,则每堆的硬币数量为 n/3 ,但是这是在 n%3==0 的情况下才能成 ...

  5. Lintcode 背包专题:最小调整代价,杆子分割,换硬币

    一.最小调整代价 给一个整数数组,调整每个数的大小,使得相邻的两个数的差不大于一个给定的整数target,调整每个数的代价为调整前后的差的绝对值,求调整代价之和最小是多少. 样例 对于数组[1, 4, ...

  6. 剑指offer_第6题_旋转数组的最小数字

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...

  7. 下行法求最小割集案例_机械产品典型失效分析案例

    原标题:机械产品典型失效分析案例 长期以来,机械结构设计习惯于传统的静强度设计,然而实际工作中,多数机械产品不属于静载工作范畴.大量的产品失效案例表明,百分之70以上的产品失效属于疲劳问题,另外有百分 ...

  8. python 最小硬币数_Python之动态规划(最少硬币数找零)

    完整代码: # 动态规划最少硬币数找零 def dpMakeChange(coinValueList, change, minCoins, coinsUsed): for cents in range ...

  9. 硬币 假硬币 天平_小东西叫硬币

    硬币 假硬币 天平 During the last 1,5 years, I've been traveling a lot. Apart from my must-have things like ...

最新文章

  1. iphone降级_今年 iPhone 将支持手写笔?乔老爷哭了!
  2. 聚合函数 -AVG/MAX/MIN/STDDEV/VARIANCE/SUM/COUNT/MEDIAN
  3. oracle 登录非系统用户,非Oracle用户使用操作系统验证登陆(/ as sysdba)
  4. 判断字符为空_49. 把字符串转换成整数(剑指offer)
  5. 【传统PSTN与互联网通信】
  6. Git [收藏链接]
  7. 在route-map中使用verify-availability确保路由可用性
  8. Java---(SpringBlade框架)后台从数据库读取所有点的经度和纬度,传输到前端显示在地图上
  9. 利用MATLAB解特征方程,并画出特征根的分布,便于分析系统的稳定性
  10. 宇宙也能测量,破解未解之谜的三维地图出炉
  11. python爬取58同城租房信息
  12. win10查看电池详细使用情况报告(查看损耗等)以及无法启动服务问题
  13. 08-SNAP的命令行处理工具gpt及其批处理(Sentinel-1和Sentinel-2为例)
  14. 【理论篇】IC间通信的时序模型——系统同步、源同步和自同步
  15. 2.springcloud配置ssh
  16. MIL/SIL/PIL/HIL/VIL
  17. Android 创建 文件 和 文件夹
  18. 自制腾讯视频去除水印Chrome插件!厉害吧!
  19. 猫狗大战——pytorch+resnet18
  20. contrast matlab,Matlab基本函数-contrast函数

热门文章

  1. 腐蚀rust服务器命令_【使用 Rust 写 Parser】2. 解析Redis协议
  2. php导出excel数据代码,phpspreadsheet导出数据到Excel的方法介绍(代码示例)
  3. vue 点击li 中的img 怎么不冒泡_Vue全解
  4. java 多个异常处理_Java 多个异常共享同一个异常处理器的方法
  5. export Oracle_sid =asm,单实例下oracle数据库从文件系统迁移到ASM上
  6. python3导入_Python3导入相对还是绝对的正确方法?
  7. Java获取上一周、上一个月、上一年的时间
  8. 18.requests
  9. 搞懂 Java HashMap 源码
  10. CentOS7:JDK1.7.0_80安装