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 ...
随机推荐
- python第三方模块的导入
模块搜索路径 当我们尝试加载一个模块时,Python会在指定的路径下搜索对应的.py文件,如果找不到,就会报错: >>> import module1 Traceback (most ...
- tcpdump抓包常用参数和用法
tcpdump 与wireshark Wireshark(以前是ethereal)是Windows下非常简单易用的抓包工具.但在Linux下很难找到一个好用的图形化抓包工具.还好有Tcpdump.我们 ...
- 18.1 volatile的作用
volatile的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 1.编译器的优化 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一 ...
- Qt对`vtable的未定义引用
错误描述:Qt在linux系统编译时,遇到一个错误大致如下形式 在 xxxxx函数中 对‘vtable for xxxxx未定义的引用 网上找了很多,各种情况都有,大多数是虚函数未实现导致的, 但也有 ...
- 彻底放弃没落的MFC,对新人的忠告!--吃瓜群众围观撕逼
http://bbs.csdn.net/topics/391817496 完全没想到10多年后还有人纠结要不要学MFC,我花点时间给新人们一个总结. 第1种观点 学习完MFC,你会更理解编程的思想,再 ...
- TCP TIME_WAIT过多的解决方法
总结: 最合适的解决方案是增加更多的四元组数目,比如,服务器监听端口,或服务器IP,让服务器能容纳足够多的TIME-WAIT状态连接.在我们常见的互联网架构中(NGINX反代跟NGINX,NGINX跟 ...
- commit lint规范化团队git提交信息
一.快速提交符号angular编码规范的提交信息 1.命令行中安装commitizen: npm install -g commitizen commitizen init cz-convention ...
- I18nUtils
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.*; ...
- SQLServer、MySQL、Oracle如何查看所有表的条数
SQLServer: create table #t(name varchar(255), rows bigint, reserved varchar(20), data varchar(20), i ...
- 安装ORACLE高可用RAC集群11g执行root脚本的输出信息
安装ORACLE高可用RAC集群11g执行root脚本的输出信息 作者:Eric 微信:loveoracle11g [root@node1 ~]# /u01/app/oraInventory/orai ...