【Leetcode】322. 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
.
Tips:给定一个coins[]数组,表示现有的硬币类型;给定一个整数amount表示要组成的金钱总数。
根据coins[],求出组成amount所需的最少的硬币数。
解法一:循环 :①初始化一个数组dp,并将数组中的值都赋值为Integer.MAX_VALUE;
② 两层循环,第一层遍历amount 第二层遍历coins[].length,当总钱数减去一个硬币的值小于0,证明无法组成该钱数,continue。或者上一步的dp值仍为初始值Integer.MAX_VALUE,也应continue;
都满足条件时,dp选择当前dp值与上一步dp值加一后的最小值,dp[i]=Math.min(dp[i],1+dp[i-coins[j]] );。
③如果无法组成amount这个钱数,就返回-1,否则返回dp[amount].
public int coinChange(int[] coins, int amount) {
int[] dp= new int[amount+1];
for(int i=0;i<=amount;i++){
dp[i]=Integer.MAX_VALUE;
}
dp[0]=0;
for(int i=1;i<=amount;i++){
for(int j=0;j<coins.length;j++){
if(i-coins[j]<0 ||dp[i-coins[j]]==Integer.MAX_VALUE) continue;
dp[i]=Math.min(dp[i],1+dp[i-coins[j]] );
}
}
return dp[amount]==Integer.MAX_VALUE?-1:dp[amount];
}
解法二: 递归 :当前组成amount所需的硬币数,与上一步有关.假设我们已经找到了能组成amount的最少硬币数,那么最后一步,我们可以选择任意的一个硬币,加入这个硬币之前,组成的金钱数为r,这时,r = amount-coins[i](需要循环所有的coins)。依次向前推,直到r等于0
或者小于0.
public int coinChange(int[] coins, int amount) {
if (amount < 0)
return 0;
return coinChangeCore(coins, amount, new int[amount]);
} private int coinChangeCore(int[] coins, int amount, int[] count) {
if (amount < 0)
return -1;
if (amount == 0)
return 0;
//剪枝
if (count[amount - 1] != 0)
return count[amount - 1];
int min = Integer.MAX_VALUE;
for (int i = 0; i < coins.length; i++) {
int ans = coinChangeCore(coins, amount - coins[i], count);
if (ans >= 0 && ans < min) {
min = 1 + ans;
}
}
count[amount - 1] = (min == Integer.MAX_VALUE) ? -1 : min;
return count[amount - 1];
}
【Leetcode】322. Coin Change的更多相关文章
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- 【LeetCode】860. Lemonade Change 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- docker 容器模式下部署mysql 主从复制
1.计划用两台host来部署,分别部署一台 mysql,一主一从,2.配置好主从mysql配置文件,更改文件名即可[client]port = 3306socket = /var/run/mysqld ...
- 我的职业规划(android)
通过一段时间的想法,自己大概圈定了自己的未来三年的职业规划,关于android的,希望大家多多批评,多多指教.或者大家也能讨论下自己对于未来的期许或者路线,虽然每个人都有自己自身的情况,但是总会有一些 ...
- go语言带cookie的net客户端请求与[]byte转string
前些日子参加了一个叫Advent of Code的编程大赛,每天一道题,快活似神仙.这每道题都有自己的拼图数据输入puzzle input,要做题就需要用到该数据,把数据复制过来感觉又太麻烦,于是就兴 ...
- HTML5 学习笔记 | LOADING...
一.HTML5基础知识 1.1 属性规范 1)元素应当合理嵌套 2)属性最好使用““包括 3)大小写不区分 1.2 常用html5标签 1)<!DOCTYPE> 定义文档类型 2)< ...
- 【LG5022】[NOIP2018]旅行
[LG5022][NOIP2018]旅行 题面 洛谷 题解 首先考虑一棵树的部分分怎么打 直接从根节点开始\(dfs\),依次选择编号最小的儿子即可 而此题是一个基环树 怎么办呢? 可以断掉环上的一条 ...
- 每日 mark
SIGNAL=${SIGNAL:-TERM} PIDS=$(jps -lm | grep -i 'kafka\.Kafka' | awk '{print $1}')if [ -z "$PID ...
- hive 日志
hive中日志分为两种: 1 系统日志,记录hive运行情况,错误状态 2 job日志 , 记录hive中 job执行的历史过程 系统日志存储位置: 配置在 hive/conf/hive-log4j. ...
- 【jQuery学习】用JavaScript写一个输出多选框的个数报错:Cannot set property 'onclick' of null"
说明:代码段来源于:<锋利的jQuery> 根据代码段我补充的代码如下: <!DOCTYPE html> <html> <head> <meta ...
- selenium自动化之定位多个元素
前面我们讲的都是如何定位单个元素,下面讲下怎么去定位多个元素,并且输出文本. 以百度为例:获取标红的这一组元素的文本 这里我用到的是xpath来定位的://div[@id="u1" ...
- Ubuntu常用shell命令
目录 ls cd mkdir mv cp scp rm df du chmod chown chgrp head tail screen apt-get Ubuntu常用shell命令 Ubuntu作 ...