[LC] 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
Note:
You may assume that you have an infinite number of each kind of coin.
- class Solution {
- public int coinChange(int[] coins, int amount) {
- if (coins == null || coins.length == 0) {
- return -1;
- }
- int[] arr = new int[amount + 1];
- for (int i = 1; i <= amount; i++) {
- arr[i] = Integer.MAX_VALUE;
- for (int j = 0; j < coins.length; j++) {
- if (i >= coins[j] && arr[i - coins[j]] != -1) {
- // like knapbag exclude the previous amoutn of i - arr[j]
- arr[i] = Math.min(arr[i], arr[i - coins[j]] + 1);
- }
- }
- arr[i] = arr[i] == Integer.MAX_VALUE ? -1 : arr[i];
- }
- return arr[amount];
- }
- }
[LC] 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 ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- 322. Coin Change
动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- 322. Coin Change选取最少的硬币凑整-背包问题变形
[抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...
- dp:322. Coin Change 自下而上的dp
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 322. Coin Change零钱兑换
网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...
随机推荐
- python刷LeetCode:14. 最长公共前缀
难度等级:简单 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- [NOI2019]弹跳(KD-Tree)
被jump送退役了,很生气. 不过切了这题也进不了队,行吧. 退役后写了一下,看到二维平面应该就是KD树,然后可以在KD树上做最短路,然后建立堆和KDTree.然后每次更新则是直接把最短路上的节点删掉 ...
- git push的时候.gitignore不起作用的解决方法
问题的原因 这是因为在你添加.gitignore之前已经进行过push操作,有些文件已经纳入版本管理了. 解决方法 我们就应该先把本地缓存删除,然后再进行git的push,这样就不会出现忽略的文件了. ...
- 学会拒绝,是一种智慧——OO电梯章节优化框架的思考
在本章的三次作业里,每次作业我都有一个主题,分别是:托盘型共享数据.单步电梯运行优化.多部电梯运行优化,因而电梯优化实际是第二.三次作业.虽然后两次作业从性能分上看做得还不错,但阅读其他大佬博客,我深 ...
- Hdu_3068 Manacger算法的心得
关于manacher算法,似乎在学完KMP之后,比较容易上手,虽然有些原理方面,我没有理解的太深. Manacher就是解决回文串的问题,求一个字符串中的最长回文子串. Manacher算法首先对字符 ...
- UVA 127 链表和栈的使用
刘汝佳的题目感觉都是比较难以处理的,就像这道题目,一看数据简直觉得头大...加上这个英文我也看的想死 最后看别人博客的题意讲解才知道原来是要移牌. 然后如果熟练的使用stack和手写链表的话,这个题目 ...
- SQL基础教程(第2版)第4章 数据更新:练习题
行也选取不出来. >> 解答 A 先生使用 BEGIN TRANSACTION 启动了事务处理,然后开始执行 INSERT 语句.因此,在 A 先生使用 COMMIT 确定该更新之前, B ...
- PAT B1095 解码PAT准考证
半个月了,每天做几道题PAT基础题,终于把基础的95道题目做完了.总体来说,没有太难的东西,偶尔几个题目有点复杂而已. 加油,离3月份的考试越来越近了,还有155道题目等着我呢!!! B_1095题目 ...
- ZJNU 2354 - 进贡
分开考虑k=1 k=2和k>=3的情况 2和3这两个质数比较特殊,遇到的话直接输出1就行 对于“神灵的不满意度为m的约数中,比m小且最大的那个”这句描述,指m除了自身和1这两个因子里找最大的那个 ...
- Ubuntu16.04编译OpenCV3.4.7
原文:https://www.bearoom.xyz/2019/08/20/ubuntu16-04-make-opencv3-4-7/ 一.前言 因为之前作死乱搞系统,然后就把Ubuntu的系统搞垮了 ...