518. Coin Change 2

coin循环要在外面 从小到大 不会有重复

 1 class Solution {
 2     public int change(int amount, int[] coins) {
 3         // if(coins.length == 0) return 1;
 4         int[] dp = new int[amount + 1];
 5         dp[0] = 1;
 6         Arrays.sort(coins);
 7         for(int j = 0; j < coins.length; j++){
 8             for(int i = 1; i <= amount; i++){
 9                 if(coins[j] <= i){
10                     dp[i] += dp[i - coins[j]];
11                 }
12             }
13         }
14         return dp[amount];
15
16     }
17 }

原文地址:https://www.cnblogs.com/goPanama/p/9870117.html

时间: 2024-08-01 10:23:44

518. Coin Change 2的相关文章

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 518. Coin Change 2

一看就是用dp来做,但是这道题还是有点意思的. dp[i] = sum_{coin} dp[i-coin] 上述的递推公式看似很对,但是会把重复的情况都考虑进去.举个例子,算dp[5]的时候,dp[5]=dp[0]+dp[4]+dp[3],但是之前算的dp[4]就是从dp[3]计算得到的. 解决办法是,一个一个硬币更新dp,即 dp[i] += dp[i-coin] for each coin.这样,对于当前的coin,dp[i-coin] 一定是不会包含当前coin的. class Solut

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

[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

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,

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.

Coin Change

322. Coin Change Total Accepted: 4182 Total Submissions: 16210 Difficulty: Medium 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