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

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. MySQL中EXPLAIN解释命令详解

    MySQL中的explain命令显示了mysql如何使用索引来处理select语句以及连接表.explain显示的信息可以帮助选择更好的索引和写出更优化的查询语句. 1.EXPLAIN的使用方法:在s ...

  2. setTimeout和setInterval的深入理解

    以前写的setTimeout和setInterval的文章有些不足之处,今天抽时间整理了一下,要想真正理解还得从javascript的单线程机制说起 大概半年前发表过一篇关于setTimeout和se ...

  3. Python学习_IDLE快捷键以及列表相关杂记

    IDLE快捷键 Tab完成:键入部分代码,按下TAB键,IDLE将给出列表帮助完成语句 回退代码语句:按下Alt+P(Previous),可以回退到IDLE中之前输入的代码语句, 下一个代码语句:按下 ...

  4. Nginx+uWSGI+Django+Python在Linux上的部署

    搞了一整天,终于以发现自己访问网络的端口是错误的结束了. 首先要安装Nginx,uWSGI,Django,Python,这些都可以再网上查到. 安装好后可以用 whereis 命令查看是否安装好了各种 ...

  5. 树莓派-交叉编译环境搭建(Eclipse)

    转自别人的文章(http://www.cnblogs.com/emouse/archive/2013/06/07/3124063.html),一些看不清楚的图片替换了一下. In this blog  ...

  6. No Hibernate Session bound to thread, and configuration does not allow creat

    No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...

  7. bzoj 3572: [Hnoi2014]世界树 虚树 && AC500

    3572: [Hnoi2014]世界树 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 520  Solved: 300[Submit][Status] ...

  8. DEEP LEARNING IS THE FUTURE: Q&A WITH NAVEEN RAO OF NERVANA SYSTEMS

    DEEP LEARNING IS THE FUTURE: Q&A WITH NAVEEN RAO OF NERVANA SYSTEMS CME Group was one of several ...

  9. 广州麒麟网络工作室 qlgame eninge(anroid) opengles c++ matrix

    在opengles中,采用的是可编程渲染管线,矩阵需要自己实现! 先说一下矩阵的理论: 参考一下资料:http://blog.sina.com.cn/s/blog_6084f588010192ug.h ...

  10. 又爱又恨的BOOTSTRAP

    搞本书,看了一天,确实,,UIKIT比它好用... 但,艺多不压身吧. 今天自己抄了个大概的,不用其它插件,,但那手风琴,真的找了很多,没有中意的... <!DOCTYPE html> & ...