动态规划--找零钱 coin change
来自http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/
对于整数N,找出N的所有零钱的表示。零钱可以用S={s1,s2,s3,..sm}表示,每种零钱的数量为无穷。请问有多少种找零的方法?
例如,
N = 4,S = {1,2,3},有四种找零方式{1,1,1,1},{1,1,2},{2,2},{1,3},return 4
N = 10,S= {2,5,3,6} ,有5中找零方式{2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5} return 5;
============
递归方法,利用搜索树
1,找出子问题模型:为了统计所有s结果的数量,我们可以将问题分为两部分: 结果中不含有硬币sm, 结果中含有硬币sm
函数int count(int S[],int m,int n)计算结果的数量,
函数返回 S[0..m-1]组成的零钱 可以为N找零钱的 方法数
那么很显然可以等价于这两者的和 count(S,m-1,n) + count(S,m,n-S[m-1]),其中count[S,m-1,n]不包括S[m-1]这个硬币,count(S,m,n-S[m-1])包括了S[m-1]这个硬币。
==
迭代计算子问题,使用递归的方法:
代码的注释是为了求解有多少找零钱的方法,
code的方法是为了求解所有结果,打印出来
class A{
public:
///returns the count of ways we can sum s[0..m-1] coins to get sum n
void help_coinChange(vector<int>& s,int m,int n,vector<int> re){
/// if n is 0 then this is 1 solution (do not include any coin)
if(n==){
for(auto i: re){cout<<i<<" ";}cout<<"-"<<endl;
return;
}
/// if n is less than 0 then no solution exists
if(n<) return;
/// if there are no coins and n is greater than 0, there no solution exist
//if(m=0 && n>=1) return ;
if(m<=) return ;
///(i) excluding S[m-1] (ii) including S[m-1]
help_coinChange(s,m-,n,re);
re.push_back(s[m-]);
help_coinChange(s,m,n-s[m-],re);
}
void coinChange(vector<int>& coins, int amount){
vector<int> re;
help_coinChange(coins,coins.size(),amount,re);
}
};
这种方法可以画成一颗DFS搜索树
例如:
c[,,],m=,n=,re=[]
/ \
c[],m=,n=,[]
/ \
c[],m=,n=,[]
/ \
c[],m=,n=,[] c[],m=,n=,[]
/ \
c[],m=,n=,[] c[],m=,n=,[,]
/ \
c[],m=,n=,[,] c[],m=,n=,[,,]
/ \
c[],m=,n=,[,,] c[],m=,n=,[,,,]
/ \
c[],m=,n=,[,,,] c[],m=1,n=0,[1,1,1,1,1]====
这么搜索,会有很多重复的路径在里面。
当然我们也可以采用BFS的方法来求解 http://bookshadow.com/weblog/2015/12/27/leetcode-coin-change/
代码在这里,
==================完全背包问题,动态规划
这个在在背包问题9讲里讲的很详细,可以google一下。
代码在这里(统计找零钱的种类数)
class A{
public:
void dp_coinChange(vector<int> & coins,int amount){
vector<int> re;
int n = amount;///
int m = coins.size();
vector<int> re1;
vector<int> re2;
///we need n+1 rows as the table is constructed in bottom up manner
/// using the base case 0 value case (n=0)
int table[n+][m];
///fill the enteries for 0 value case (n = 0)
for(int i = ;i<m;i++){
table[][i] = ;
}
///fill rest the table enteries in bottom up manner
for(int i = ;i< n+;i++){
for(int j = ;j<m;j++){
///count of solutions including S[j]
int x = (i-coins[j] >=)? table[i-coins[j]][j]:;
///count of solutions excluding S[j]
int y = (j >= )?table[i][j-]:;
///total count
table[i][j] = x+y;
}cout<<endl;
}
//return table[n][m-1];
}
};
==================================
动态规划寻找零钱个数最少的解。leetecode 322
给定几个固定的面值,可以无限使用。一个目标数,要求用最少的硬币兑换这个target
解法1,如果每次走给定面值的步数,问对少走多少步能达到目标target,可以使用bfs的思路求解。
解法2,动态规划:dp[i] = min{dp[i-c1],dp[i-c2],dp[i-c3],dp[i-c4]...}+1
class Solution {
public:
int coinChange(vector<int>& coins, int amount){
///dp[i] = min{dp[i-c1],dp[i-c2],dp[i-c3],dp[i-c4]...}+1
if(amount == ) return ;
vector<int> dp(amount+,INT_MAX);
dp[] = ;
for(int i = ;i<amount+;i++){
for(int j = ;j<(int)coins.size();j++){
if(i-coins[j]>=){
dp[i] = min(dp[i],dp[i-coins[j]]);
}
}
int tmp = (dp[i]==INT_MAX)? INT_MAX: dp[i]+;
dp[i] = tmp;
}
return dp[amount]==INT_MAX? -: dp[amount];
}
};
动态规划--找零钱 coin change的更多相关文章
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] 518. Coin Change 2 硬币找零 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- python---通过递归和动态规划策略解决找零钱问题
也是常见套路. # coding = utf-8 def rec_mc(coin_value_list, change, know_results): min_coins = change if ch ...
- 后台开发 3个题目 array_chunk, 100块钱找零钱(动态规划 dynamic programming), 双向循环链表 llist 删除节点
1. array_chunk 实现 http://php.net/manual/en/function.array-chunk.php <?php function my_array_chunk ...
- [LeetCode] Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] 由 “找零钱" 所想
Ref: [Optimization] Dynamic programming[寻找子问题] Ref: [Optimization] Advanced Dynamic programming[优于re ...
- JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)
JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划) B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...
随机推荐
- linux 命令——23 目录结构
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...
- bzoj3312: [Usaco2013 Nov]No Change
题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...
- 【洛谷2051】[AHOI2009] 中国象棋(烦人的动态规划)
点此看题面 大致题意: 让你在一张\(N*M\)的棋盘上摆放炮,使其无法互相攻击,问有多少种摆法. 辟谣 听某大佬说这是一道状压\(DP\)题,于是兴冲冲地去做,看完数据范围彻底懵了:\(N≤100\ ...
- iptables 防火墙详解
一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防 ...
- IIS6.0开启gzip压缩
双击IIS服务器,右键点击网站,点击属性,然后点击服务,我们看到HTTP压缩,然后在压缩应用程序文件,压缩静态文件中打钩,然后点击确定,第一步就完成了 然后我们右键点击web服务扩展,点击添加一个 ...
- linux用命令行运行matlab的.mat文件
入m文件所在目录后,运行 $ matlab -nodesktop -nosplash -r matlabfile 只用文件名matlabfile,不能添加.m
- GBDT回归的原理及Python实现
一.原理篇 1.1 温故知新回归树是GBDT的基础,之前的一篇文章曾经讲过回归树的原理和实现.链接如下: 回归树的原理及Python实现 1.2 预测年龄仍然以预测同事年龄来举例,从<回归树&g ...
- scikit-learn 中 OneHotEncoder 解析
概要 在 sklearn 包中,OneHotEncoder 函数非常实用,它可以实现将分类特征的每个元素转化为一个可以用来计算的值.本篇详细讲解该函数的用法,也可以参考官网 sklearn.prepr ...
- IE脚本调试
打开IE -- 工具 -- Internet选项 -- 高级 --有4项. 1.禁用脚本调试(Internet Explorer)(去掉对勾) 2.禁用脚本调试(其他)(去掉对勾) 3.显示每个脚本错 ...
- C的xml编程-libxml2
这里主要讲述libxml2在linux下的使用. (以下内容除了linux下的安装步骤是自己写的,其余均出自http://www.blogjava.net/wxb_nudt/archive/2007/ ...