零钱兑换

作者: windUtterance | 来源:发表于2020-09-16 19:33 被阅读0次

题目描述
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。

示例
输入: coins = [1, 2, 5], amount = 11
输出: 3
解释: 11 = 5 + 5 + 1

Java代码

class Solution {
    int res = Integer.MAX_VALUE;
    public int coinChange(int[] coins, int amount){
        if(amount==0) return 0;
        Arrays.sort(coins);
        mincoin(coins,amount,coins.length-1,0);
        return res==Integer.MAX_VALUE? -1:res;
    }
    private void mincoin(int[] coins,int amount, int index, int count){
        if(amount==0){
            res = Math.min(res,count);
            return;
        }
        if(index<0){
            return;
        }
        for(int i = amount/coins[index];i>=0 && i+count<res; i--){
            mincoin(coins,amount - (i*coins[index]), index-1, count+i);
        }
    }
}

相关文章

  • LeetCode-322-零钱兑换

    LeetCode-322-零钱兑换 322. 零钱兑换[https://leetcode-cn.com/probl...

  • LeetCode 零钱兑换 背包问题

    题目地址:322.零钱兑换 leetcode地址518.零钱兑换2 leetcode地址类似题目:123.股票问题...

  • 动态规划

    1. 零钱兑换 零钱兑换 (Medium) 力扣 题目描述:给定不同面额的硬币 coins 和一个总金额 amou...

  • 零钱兑换

    给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。...

  • 零钱兑换

    问题描述 给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的...

  • 零钱兑换

    题目描述:给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的...

  • 零钱兑换

    题目来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/coin...

  • 零钱兑换

    题目: 题目的理解: 看似很简单的题目,用了一天时间编写算法,但是结果是一直计算超时,!_!参考了其他的解题思路,...

  • 零钱兑换

    题目地址:https://leetcode-cn.com/problems/coin-change/题解: 一.思...

  • 零钱兑换

网友评论

    本文标题:零钱兑换

    本文链接:https://www.haomeiwen.com/subject/pdblektx.html