动态规划里例题,硬币问题。

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的更多相关文章

  1. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  2. [LeetCode] 322. Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  3. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  4. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

  5. [LC] 322. Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  6. dp:322. Coin Change 自下而上的dp

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  7. 【LeetCode】322. Coin Change 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. 322. Coin Change零钱兑换

    网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...

  9. 322. Coin Change选取最少的硬币凑整-背包问题变形

    [抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...

随机推荐

  1. PL/SQL学习(五)异常处理

    原文参考:http://plsql-tutorial.com/ 组成: 1) 异常类型 2) 错误码 3) 错误信息   代码结构: DECLARE Declaration section BEGIN ...

  2. mvc中的OutputCache

    mvc4中有一个标记属性OutputCache,用来对ActionResult结果进行缓存,如何理解呢?概括地说,就是当你的请求参数没有发生变化时,直接从缓存中取结果,不会再走服务端的Action代码 ...

  3. python【第二十篇】Django表的多对多、Ajax

    1 创建多对多表的方式有两种 1.1 方式一:自定义关系表 class Host(models.Model): nid = models.AutoField(primary_key=True) hos ...

  4. 查看Oracle表空间使用情况与增大表空间

    1,查看表空间使用情况 SELECT D.TABLESPACE_NAME, SPACE || 'M' "SUM_SPACE(M)", BLOCKS "SUM_BLOCKS ...

  5. SQL 多条件查询

    网上有不少人提出过类似的问题:“看到有人写了WHERE 1=1这样的SQL,到底是什么意思?”.其实使用这种用法的开发人员一般都是在使用动态组装的SQL.让我们想像如下的场景:用户要求提供一个灵活的查 ...

  6. [转]jquery.timer用法

    转自:http://www.cnblogs.com/guohui/archive/2012/02/24/2366668.html 来自JavaEye论坛的JQuery Timers应用知识 jQuer ...

  7. 每天学点管理学知识——情绪ABC理论

    什么是ABC理论 ABC理论(ABC Theory of Emotion)是由美国心理学家埃利斯创建的.就是认为激发事件A(activating event 的第一个英文字母)只是引发情绪和行为后果C ...

  8. JVM 学习笔记

    1.   JAVA类分为三类: 1.1   系统类  (用系统类加载器加载bootstrap ClassLoader) 1.2   扩展类  (用扩展类加载器加载Ext ClassLoader) 1. ...

  9. [Android应用]《花界》V1.0 正式版隆重发布!

    http://www.cnblogs.com/qianxudetianxia/archive/2012/04/05/2433669.html 1. 软件说明(1). 花界是一款看花软件:“看花,议花, ...

  10. OneAlert 入门(二)——事件分析

    OneAlert 是国内首个 SaaS 模式的云告警平台,集成国内外主流监控/支撑系统,实现一个平台上集中处理所有 IT 事件,提升 IT 可靠性.有了 OneAlert,你可以更快更合理地为事件划分 ...