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:
Input: coins =[1, 2, 5], amount =11
Output:3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins =[2], amount =3
Output: -1
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
dp数组运行了之后才有正常值,所以需要用arrays.fill初始化为奇葩值
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
Math.min(dp[目标值], dp[目标值 - 当前值] + 1);
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
记得初始化
[复杂度]:Time complexity: O(n2) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
class Solution {
public int coinChange(int[] coins, int amount) {
//corner case
if (coins == null || coins.length <= 0 || amount <= 0) return -1;
//initialization: dp[] and fill, and the first num
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
//for loop for each coins, each amount
for (int j = 0; j < coins.length; j++) {
for (int i = 0; i <= amount; i++) {
if (i - coins[j] >= 0)
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
//return if dp[] is normal value, or just -1
return dp[amount] > amount ? -1 : dp[amount];
}
}
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
322. Coin Change选取最少的硬币凑整-背包问题变形的更多相关文章
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 322 Coin Change 零钱兑换
给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...
- 【Leetcode】322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 用背包问题思路解决 322. Coin Change(完全背包)
首先需要明白 0-1 背包问题中的放置表格,见 “玩转算法面试 从真题到思维全面提升算法思维” 9-5 节,本题思路类似表格纵向为:只考虑第 [0 …,… index] 种硬币(物品)表格横向为:需要 ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- 322. Coin Change零钱兑换
网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 322. Coin Change
动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...
随机推荐
- hsdfz -- 6.17 -- day2
今日依旧康复…… 当天晚上被老师拉去小吃街了,晚上回来精力憔悴,所以并没有当天写 反正就惨,因为估错复杂度,期望得分100分最后结果20分 (我的复杂度是nlog^2n的,正确性有保障,稳! 事后:还 ...
- BEAM188简单应用
目录 BEAM188简介 APDL应用实例 显示梁三维图 BEAM188简介 BEAM188-3D线性有限应变梁 Beam188 单元适合于分析从细长到中等粗短的梁结构,该单元基于铁木辛哥梁结构理论, ...
- ajax 调用webservice 跨域问题
注意两点 1. 在webservice的config中加入这段位置 (注意不是调用webservice的webconfig中加入) <system.webServer> <! ...
- 互联网同步yum服务器阿里云 reposync createrepo
参考文章: https://www.cnblogs.com/lldsn/p/10479493.html 系统版本centos 7.5 最小化安装 修改主机名 hostnamectl set-hostn ...
- 【算法和数据结构】_15_小算法_打印EOF的值
/* 本程序打印EOF的值 */ #include <stdio.h> int main(int argc,char* argv[],char* env) { printf("E ...
- 我是一个录像机(NVR)
我是一个网络录像机,简称NVR.我的前辈是DVR,我们的区别很简单,DVR接的是模拟摄像机,我连接的是IP摄像机. 我的前辈DVR比我辛苦,因为模拟摄像机的模拟信号连过来之后,他要进行数字化.编码压缩 ...
- js中记住密码功能
js中记住密码功能(在前端实现) 直接上例子(如果你也要实现的话注意改一些变量名称,jsp代码不包含样式) Jsp代码: <form class="am-form tpl-form-l ...
- windows下安装Kettle
先下载kettle稳定版本https://community.hitachivantara.com/docs/DOC-1009855 下载后并且解压 安装kettle必须安装jdk在你的电脑上,ket ...
- java面试题收集
http://www.cnblogs.com/yhason/archive/2012/06/07/2540743.html 2,java常见面试题 http://www.cnblogs.com/yha ...
- 一针见血tomcat
一针见血tomcat 一.Tomcat各组件认知 1 Tomcat架构说明 Tomcat是一个基于JAVA的WEB容器,其实现了JAVA EE中的 Servlet 与 jsp 规范,与Nginx ap ...