Coin change
来源:互联网 发布:看本子的软件 编辑:程序博客网 时间:2024/05/22 10:35
http://www.fgdsb.com/2015/01/03/coin-change-problem/#more
Given a set of currency denominations, find the minimum number of coins needed to represent an amount.
1234567891011121314
int minCoins(int a[], int n, int t){vector<int> dp(t+1,0);int minVal;for(int i = 1; i <= t; i++){minVal = i;for (int j = 0; j < n; j++)if(a[j] <= i)minVal = min(dp[i-a[j]]+1,minVal);elsebreak;dp[i] = minVal;}return dp[t];}
Follow Up:
How many ways can we make the change?
123456789101112131415161718
int count( int S[], int m, int n ) {// table[i] will be storing the number of solutions for// value i. We need n+1 rows as the table is consturcted// in bottom up manner using the base case (n = 0)vector<int> table(n+1, 0);// Base case (If given value is 0)table[0] = 1;// Pick all coins one by one and update the table[] values// after the index greater than or equal to the value of the// picked coinfor(int i=0; i<m; i++)for(int j=S[i]; j<=n; j++)table[j] += table[j-S[i]];return table[n];}
0 0
- Coin Change
- Coin Change
- Coin change
- Coin Change
- Coin change
- Coin Change
- Coin Change
- Coin Change
- Coin Change
- Coin Change
- Coin Change
- Coin Change
- Coin Change
- Coin Change
- Coin Change
- Coin Change
- coin change
- Coin Change
- 3Sum 3个数的和为0 遍历+左右循环
- 关于高质量PCB设计经验总结
- 集线器交换机共享带宽独享带宽的理解
- 玩转字符串
- linux下注销其它用户或者tty的命令
- Coin change
- 求两个数组的交集
- 判断网络
- RAC 实例迁移到单实例 -- 使用RMAN Duplicate
- python定义语言级的全局函数
- 3Sum Closest 3个数字的和等于或者最接近与目标数
- 写博客的种种好处
- apk反编译
- 人生第一笔试加面试 SAP胜略