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
想法:采用动态规划,状态转移表达式为result[amount]=result[amount-coins]+1;
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int len = coins.size();
        vector<);
        result[] = ;
        int max = INT_MAX;
         ; i <= amount ;i++ ){
            result[i] = max;
             ; j < coins.size() ; j++){
                <result[i]){
                    result[i] = result[i-coins[j]] + ;
                }
            }
        }
        if(result[amount] == max){
            ;
        }else{
            return result[amount];
        }
    }
};

leetcode322—Coin Change的更多相关文章

  1. leetcode322 Coin Change

    """ You are given coins of different denominations and a total amount of money amount ...

  2. Leetcode322. Coin Change零钱兑换

    给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coins = ...

  3. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  4. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 2069 Coin Change

    Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

  7. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

  8. C - Coin Change (III)(多重背包 二进制优化)

    C - Coin Change (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. Coin Change (IV) (dfs)

    Coin Change (IV) Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu [Subm ...

随机推荐

  1. A simple Gaussian elimination problem.(hdu4975)网络流+最大流

    A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...

  2. Java - LinkedList源码分析

    java提高篇(二二)---LinkedList 一.概述 LinkedList与ArrayList一样实现List接口,只是ArrayList是List接口的大小可变数组的实现,LinkedList ...

  3. Linux常用基本命令( tree, pwd, cd )

    pwd与cd命令 >pwd命令是“print working directory”中每个单词的首字母缩写,其功能是显示当前工作目录的绝对路径.在实际工作中,我们在命令行操作命令时,经常会在各个目 ...

  4. JSON 解析与封装

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.解析: var str = '{"name":"huangxiaojian&qu ...

  5. 【代码笔记】iOS-4个可以单独点击的button

    一,效果图. 二,工程图. 三,代码. ViewController.m #import "ViewController.h" @interface ViewController ...

  6. H5 canvas 实现飞机大战游戏

    首先看几张效果图: 上面三张图分别对应游戏的三种状态 ready,play,pause.体验一下 先介绍一下canvas 画图的原理,在这个游戏中的背景,飞机,子弹以及飞机被击中爆炸的效果都是一张张的 ...

  7. 排错-Ad Hoc Distributed Queries组件被禁用的解决办法

    Ad Hoc Distributed Queries组件被禁用的解决办法 by:授客 QQ:1033553122 SQL Server 阻止了对组件 'Ad Hoc Distributed Queri ...

  8. 如何在单元测试时隔离ORM

    在项目中需要对DAL层进行单元测试,如果直接操作数据库,首先测试速度会大大下降,而且让单元测试直接使用外部依赖,很可能带来后续维护的不便,所以有必要对数据库隔离,然后单独测试DAL层.由于使用了ORM ...

  9. unityShader CGINCLUDE关键字

    unityshader中经常见到CGPROGRAM,除此之外还有一个CGINCLUDE关键字. 二个关键字都是用ENDCG来结束,但是用法完全不一样. CGINCLUDE和ENDCG内可以插入一些sh ...

  10. [Android] 修图工具Draw9patch使用小结(附ubuntu快捷截图方法)

    做项目的时候,素材图遇到点问题,然后老大大概给我讲了讲android下面图片格式.9.png和draw 9-patch的用法,感觉很清楚也很有用,所以记录一下. 原文地址请保留http://www.c ...