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

p[i] = dp[i - coin[j]] + 1;

注意i < coin[j] dp[i-coin[j]]无解都要跳过。

  1. public class Solution
  2. {
  3. public int coinChange(int[] coins, int amount)
  4. {
  5. int[] dp = new int[amount+1];
  6. Arrays.fill(dp,Integer.MAX_VALUE);
  7. //dp[i] = dp[i - coin[j]] + 1;
  8. dp[0] = 0;
  9. for(int i = 0; i <= amount; i++)
  10. {
  11. for(int j = 0; j < coins.length;j++)
  12. {
  13. if(i < coins[j] || dp[i-coins[j]] == Integer.MAX_VALUE) continue;
  14. dp[i] = Math.min(dp[i],dp[i - coins[j]]+1);
  15. }
  16. }
  17. if(dp[amount] == Integer.MAX_VALUE) return -1;
  18. else return dp[amount];
  19. }
  20. }

第一次看到动态规划觉得好神奇。。

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. python連接mysql數據庫

    第一步,安裝mysql數據庫. 這裏我安裝的是mariadb數據庫,版本5.5,並且配置好了字符集.此處不詳細敘述,相信大家沒有問題. 第二步,安裝mysql驅動. 首先說明一下有兩個主要的驅動: m ...

  2. sql server2005主从数据库同步配置

    网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库.主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据的 ...

  3. [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】

    题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...

  4. 如何解决jquery版本冲突

    <!-- 引入1.6.4版的jq --> <script src="<a href="http://ajax.googleapis.com/ajax/lib ...

  5. 教你如何监控 Apache?

    什么是 Apache? Apache 是一款 HTTP 服务器软件,现在更名为 "http",而 Apache 则成了一个(包含httpd的项目)巨大的基金组织,根据习惯后文都用 ...

  6. 今天,安装了一个GANGLIA玩玩,以后再测试NAGIOS吧。

    说不定以后派得上用场呢.. 还有,NGINX也要学,不能老是凭站IIS,APACHE混饭吃吧,现在它都这么流行了..新浪,网易,腾讯.

  7. 【BZOJ 3110】 [Zjoi2013]K大数查询(整体二分)

    [题目] Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到 ...

  8. ANDROID_MARS学习笔记_S04_003_用HttpClent发http请求

    一.代码 1.xml(1)activity_main.xml <TextView android:layout_width="wrap_content" android:la ...

  9. 运行所选代码生成器时出错:无效指针(异常来自HRESULT:0x80004003(E_POINTER))

    这个是在使用了VS2015 update1学MVC的时候,在controllers的方法添加view时报的一个错误,中文基本搜不到解决方法,然后无奈转到成英文,还好G家的搜索提示补全能力拯救了我的渣英 ...

  10. 计算机中的大小端模式及C语言中如何鉴别他们

    我的博客:www.while0.com 参考http://blog.csdn.net/ce123_zhouwei/article/details/6971544 写的很详细. 大小端主要是对数字类型来 ...