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 ...
随机推荐
- pass
空语句 do nothing 保证格式完整 保证语义完整 以if语句为例,在c或c++/java中: if(true) ; //do nothing else { //do something } 1 ...
- 虚拟化cpu
vmware的虚拟机cpu [root@84-monitor ~]# lscpuArchitecture: x86_64CPU op-mode(s): 32-bit, ...
- Object.is()
- 1.2 VMware安装过程
1.新建虚拟机后,选择[我以后再安装],否则安装过程看不到. 2.在虚拟机设置里,内存设置不能超过物理内存的一半. 3.创建硬盘 4.设置CD/ROM使用ISO镜像文件 5.创建[快照] 6.克隆虚拟 ...
- py-day2-4 python 集合
# 集合是由 { ,} 组成 test = {1,2,8,9,7,5} print(test) {1, 2, 5, 7, 8, 9} # 集合的结果是 去重的,且排序是 无序的 test = {1,2 ...
- 关于DDR3控制器的使用
关于DDR3控制器的使用 mig_7series_0 u_mig_7series_0 ( // Memory interface ports .ddr3_addr (ddr3_addr), // ou ...
- [UE4]List View
List View适合显示数以千条的列表 要点 一.创建Entry类,实现IUserObjectListEntry. 创建一个名为“EntryWidget”的UserWidget,添加如下图所示的控件 ...
- 2017-2018-2 20165312实验二《Java面向对象程序设计》实验报告
2017-2018-2 20165312实验二<Java面向对象程序设计>实验报告 实验中遇到的问题 1.增加MyUtil的测试类之后,TestCase是红色的,但是没有找到junit.j ...
- Python中print字体颜色的设置
Python中print字体颜色的设置 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以ESC开头,即用\033来完成 ...
- C# WinForm 实现窗体淡入淡出
有时候我们需要给窗体的打开和关闭添加点动画效果.最近正好有这类需求,于是研究了下窗体的淡入淡出,很简单就实现了,这里发表下成果,以供朋友们使用. 在Windows,有一个API,可以设置窗体的可见度, ...