322. Coin Change
动态规划里例题,硬币问题。
p[i] = dp[i - coin[j]] + 1;
注意i < coin[j] dp[i-coin[j]]无解都要跳过。
public class Solution
{
public int coinChange(int[] coins, int amount)
{
int[] dp = new int[amount+1];
Arrays.fill(dp,Integer.MAX_VALUE);
//dp[i] = dp[i - coin[j]] + 1;
dp[0] = 0;
for(int i = 0; i <= amount; i++)
{
for(int j = 0; j < coins.length;j++)
{
if(i < coins[j] || dp[i-coins[j]] == Integer.MAX_VALUE) continue;
dp[i] = Math.min(dp[i],dp[i - coins[j]]+1);
}
}
if(dp[amount] == Integer.MAX_VALUE) return -1;
else return dp[amount];
}
}
第一次看到动态规划觉得好神奇。。
322. Coin Change的更多相关文章
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- [LC] 322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- dp:322. Coin Change 自下而上的dp
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 322. Coin Change零钱兑换
网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...
- 322. Coin Change选取最少的硬币凑整-背包问题变形
[抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...
随机推荐
- LOL(英雄联盟)系统鼠标速度锁定工具
最近习惯将系统的鼠标速度降低, 而提高鼠标硬件DPI来提升移动准确度, 但是LOL的客户端每次启动进入游戏后就会还原系统鼠标的移动速度, 我把情况反应给腾讯,没想到他们一点都不重视, 建议我只改游戏里 ...
- python学习_数据处理编程实例(一)
目的:用一个实例总结学习到的with语句,函数,列表推导,集合,排序,字符分割等内容 要求:分别以james,julie,mikey,sarah四个学生的名字建立文本文件,分别存储各自的成绩,时间格式 ...
- 2016-10-31 reload
经历了创业公司的欠薪后,决定重新开始,从原本着力于如何快速入门与实现变更为巩固基础再出发. 2016-10-31
- C#中指针使用总结
C#为了类型安全,默认并不支持指针.但是也并不是说C#不支持指针,我们可以使用unsafe关键词,开启不安全代码(unsafe code)开发模式.在不安全模式下,我们可以直接操作内存,这样就可以使用 ...
- C语言面向对象的简便方法
都知道C语言是面向过程的,但是现在软件规模越来越大,通过面向对象的方式可以简化开发.业余时间想了个简单的方法,在C中使用一部分面向对象的基本功能.由于C语言自身的限制,并不完善,只能将就用,聊胜于无, ...
- windows 远程桌面连接ubuntu xrdp 只看到墙纸其他什么都没有
用 windows 的 mstsc 连接 ubuntu 的 xrdp 时,进入后只看到墙纸,其他什么都没有,鼠标指针也不见,输入按键都无反应. 原来 Ubuntu 启动了 3d 桌面,导致 xrdp ...
- 2D image convolution
在学习cnn的过程中,对convolution的概念真的很是模糊,本来在学习图像处理的过程中,已对convolution有所了解,它与correlation是有不同的,因为convolution = ...
- ACM俱乐部算法基础练习赛(1)
A: 水题 代码: #include<cstdio> #include<algorithm> using namespace std; ]; int n,m,c; int ma ...
- Performance Test of List<T>, LinkedList<T>, Queue<T>, ConcurrentQueue<T>
//Test Group 1 { var watch = Stopwatch.StartNew(); var list = new List<int>(); ; j < ; j++) ...
- easyui datagrid单击单元格选择此列
示例代码实现单击jquery easyui datagrid的单元格时,取消datagrid默认选中高亮此行的样式,改为选中单击的单元格所在的列,高亮此列上的所有单元格.可以配置全局single变量, ...