【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 ...
随机推荐
- 实验一:实现求正整数1-N之间所有质数的功能,并进行测试。
实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 命令行下的程序开 ...
- noone is not in the sudoers file ubuntu
Login as root or su to get root prompt type visudo an editor will open find a line says root ALL=( ...
- WPF 如何自定义一个弹框
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 简述: 手工以原生Grid的方式,自定义了一个仿弹窗效果,优点可以自定义,缺点需要自己实现以及维护整个弹窗的效 ...
- DSP5509项目之用FFT识别钢琴音调(5)之开始傅里叶变换
1. 首先电脑上下载一个音频模拟的软件 2. 研究下钢琴的声音范围27HZ到4000HZ,那么采样频率需要是信号的两倍频率以上,所以建议采样频率是16KHZ.先看一下采集到的数据,如下是空载时候采集到 ...
- 使用SCSS扩展Bootstrap4
摘要 因为打算写一个小网站,而个人时间又不是那么充裕,所以没有选择前后端分离的架构. 对于非前后端分离应用来说,Bootstrap应该是目前的最佳前端框架之一了. 而Bootstrap4,是Boots ...
- Ubuntu 安装python后,安装python-dev
1.通常情况下: sudo apt install python-dev 或者 在 sudo apt install python 命令下安装应该也附带了 python-dev 上述 pyhthon ...
- thinkphp5登录并保存session、根据不同用户权限跳转不同页面
本文讲述如何在thinkphp5中完成登录并保存session.然后根据不同的用户权限跳转相应页面功能的实现.我也在学习thinkphp源码的路上,记录一下并与大家分享.完成该步骤主要有以下三个步骤完 ...
- spark重点知识
1 scala 2 spark rdd 3 sprak core 4 性能优化 5 spark sql 6 spark streaming 掌握程度-精通的理解:
- 【TCP_协议_socket接口】-jmeter
1.ip 2.端口号 3.传入参数 4.告诉软件返回 最后以为是什么,不然就会报错 或者无限制的等待 查ascll 码表 启动接口的方法
- 228. [LeetCode] Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...