原题链接在这里:https://leetcode.com/problems/coin-change/

题目:

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)

Example 2:
coins = [2], amount = 3
return -1.

题解:

要求满足amount时用的最少coins数目.

Let dp[i] denotes amount == i 时用的最少coins数目. 用一维array储存.

递推时,状态转移dp[i] = Math.min(dp[i], dp[i-coins[j]]+1).  dp[i-coins[j]]+1表示用最少个数coin表示i-coins[j].

初始化dp[0] = 0. amount为0时不需要硬币. dp其他值都是Integer的最大值, 之后会每次更新取最小值.

Time Complexity: O(amount * coins.length).

Space: O(amount).

AC Java:

class Solution {
public int coinChange(int[] coins, int amount) {
if(amount == 0){
return 0;
} if(coins == null || coins.length == 0){
return -1;
} int [] dp = new int[amount+1];
dp[0] = 0;
for(int i = 1; i<=amount; i++){
dp[i] = Integer.MAX_VALUE;
for(int coin : coins){
if(i-coin<0 || dp[i-coin]==Integer.MAX_VALUE){
continue;
}else{
dp[i] = Math.min(dp[i], dp[i-coin]+1);
}
}
} return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}

类似Minimum Cost For Tickets.

LeetCode Coin Change的更多相关文章

  1. [LeetCode] Coin Change 硬币找零

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

  2. [LeetCode] Coin Change 2 硬币找零之二

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

  3. LeetCode——Coin Change

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

  4. LeetCode OJ 322. Coin Change DP求解

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

  5. [LeetCode] 518. Coin Change 2 硬币找零之二

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

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

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

  7. [LeetCode] 518. Coin Change 2 硬币找零 2

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

  8. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  9. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. C# 如何用多个字符串来切分字符串并去除空格

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  2. [BI项目记]-DB脚本同步

    BI项目中会有很多不同种类的项目,其中比较比较大的一部分就是对DB脚本的处理.然而DB的脚本毕竟无法在项目中进行维护,所以这里介绍如何对DB的脚本进行版本上的维护. 数据库脚本本身没有项目模板支持,很 ...

  3. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

    问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  4. 安装mcrypt

    Mcrypt扩展是 mcrypt 库的接口,mcrypt 库提供了对多种块算法的支持. 安装mcrypt之前请确认已经安装yum install gcc php-devel 执行命令:yum upda ...

  5. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  6. haohantech浩瀚盘点机“PDA无线订货开单”终端 移动现场下单APP(打印扫描一体)

    手持PDA盘点机,订货的时候,用PDA上自带的激光扫描头扫描(或手输)样品的条码,然后,只需输入该款产品不同尺码的数量即可自动(或手动)发送订货数据到总部服务器.盘点机“PDA无线订货”终端功能: 1 ...

  7. TMS 例子63 分组,子node

    procedure TForm1.InitGrid; begin advstringgrid1.Grouping.MergeHeader := true; //这个什么作用没有是 advstringg ...

  8. T-SQL Recipes之生成动态列表数据

    Problem 首先什么是动态列表?举个示例,假设你想输出以逗号分隔的IDs,如: 1,45,67,199,298 Solution 生成动态列表数据在我们生活场景中很常见,比如在 Dynamic P ...

  9. java调用url接口

    很多简单的接口就是直接一个URl的形式, 怎么调用? HttpClient httpclient=null; PostMethod post=null; try{ httpclient = new H ...

  10. 利用activeX控件在网页里自动登录WIN2003远程桌面并实时控制

    首先要自己配置并打开受控端的WEB远程桌面服务,这个在“添加/删除windows组件”里有,我只在windows 2003 server里试过,没试过XP.下面我们在客户端安装微软提供的远程桌面客户端 ...