[LeetCode] 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:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)

Example 2:
coins = [2], amount = 3
return -1.

Note:
You may assume that you have an infinite number of each kind of coin.

很经典的题目了,但是搞起来还是有点费力的。

这题是要求全局最优,用贪心搞得话数据好一点是过不了的,唯有用dp搞了

最重要的一点是在状态转移的时候 先要判断 ret[i-coins[j]]是否可以找开,找不开就不搞。

Ps.可能可以简化一下代码,但是当时没想太多就写了

public class Solution {

    public int coinChange( int[] coins, int amount ) {
        if(amount==0) return 0;
        int[] ret = new int[ amount + 1 ];
        for( int i = 1; i <= amount; i++ ) {
            int min = i;
            for( int j = 0; j < coins.length; j++ ) {
                if( i >= coins[ j ] ) {
                if(i==coins[j]){
                    ret[i] = 1;
                }                //这里判断一下是否可以找开
                else if(ret[i-coins[j]]>0){
                        if(i%coins[j]==0){
                        ret[i] = Math.min(ret[i-coins[j]]+1,i/coins[j]);
                        }else{
                            ret[i] = ret[i-coins[j]] +1;
                        }
                         if(ret[i]<min){
                            min = ret[i];
                        }
                        ret[i] = min;
                    }else{
                        ret[i-coins[j]]=-1;
                    }
                }
            }
        }
        if(ret[amount]==0) return -1;
        return ret[ amount ];
    }

    public static void main( String[] args ) {
        Solution s = new Solution();
        int[] coins = new int[] { 1,2 };
        int amount = 3;
        System.out.println( s.coinChange( coins, amount ) );
    }

}
时间: 2024-10-26 00:34:28

[LeetCode] Coin Change的相关文章

[LeetCode] 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,

[LeetCode][JavaScript]Coin Change

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 o

LeetCode OJ 322. Coin Change DP求解

题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accepted: 15289 Total Submissions: 62250 Difficulty: Medium You are given coins of different denominations and a total amount of money amount. Write a func

leetcode 518. Coin Change 2/硬币找零 2

归纳于http://www.cnblogs.com/grandyang/p/7669088.html 原题https://leetcode.com/problems/coin-change-2/description/ 518. Coin Change 2(Medium) You are given coins of different denominations and a total amount of money. Write a function to compute the numbe

Leetcode OJ --- 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,

dp背包问题/01背包,完全背包,多重背包,/coin change算法求花硬币的种类数

一步一步循序渐进. Coin Change 具体思想:给你 N元,然后你有几种零钱S={S1,S2...,Sm} (每种零钱数量不限). 问:凑成N有多少种组合方式  即N=x1 * S1+x2*S2+...+xk*Sk (xk>=0,k=1,2..m) 设有f(x)中组合方式 有两种解答(自底向上回溯): 1.不用第m种货币   f(N,m-1) 2.用第m种货币 f(N-Sm,m) 总的组合方式为f(N,m)=f(N,m-1)+f(N-Sm,m) anything is nonsense,s

HDU 2069 Coin Change

Coin Change Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 206964-bit integer IO format: %I64d      Java class name: Main Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We

HDU 2069 Coin Change 母函数求解

HDU 2069 Coin Change 母函数求解 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2069 这题比普通的母函数求解题目复杂一点,多了组成个数的限制, 要求组合个数不能超过 100 个硬币,所以将 c1, c2 定义为二维数组, c1[i][j] 表示 c1[结果][组成该结果的个数] ,然后多了一层遍历个数的循环. // 母函数求解 #include <bits/stdc++.h> using namespace std; cons

hdu 2069 Coin Change(完全背包)

Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16592 Accepted Submission(s): 5656 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent.