322. Coin Change

ref: https://leetcode.com/problems/coin-change/



就是完全背包问题,可以再复习一遍背包问题。

01背包:

for 每一个item

  for amount...cost[item]

    f[v] = Max{f[v], f[v-cost[item]] + weight[item]}

因为01背包每个item只能用一次,所以amount要从后往前算,因为比较的时候用的是f[v]和f[v-cost[item]],第二个那里是减号,所以退着算,后面的结果前面没有使用

完全背包

for 每一个item

  for cost[item]...amout

    f[v] = Max{f[v], f[v-cost[item]] + weight[item]}

因为每一个物品可以用很多次,所以amount从小往大走,因为每一次后面的结果都用到了前面的结果。

如果要求满背包,就把f[0]设置为0,其他都设置为Integer.MIN_VALUE



下面讨论本题,本题是一个完全背包问题,coins[]就是cost[],amount就是背包的容量,要求背包刚好装满,weight是所有的item都为1

区别是传统背包问题是要求最大weight,但是本题是求最小weight,所以在初始化上有区别。dp[0] = 0, 剩下的都是Integer.MAX_VALUE

 1     public int coinChange(int[] coins, int amount) {
 2         if (coins.length == 0 || amount < 0) {
 3             return -1;
 4         }
 5         int[] dp = new int[amount+1];
 6         Arrays.fill(dp, Integer.MAX_VALUE);
 7         dp[0] = 0;
 8         for (int i = coins.length - 1; i >= 0; i--) {
 9             for (int j = coins[i]; j <= amount; j++) {
10                 if (dp[j - coins[i]] != Integer.MAX_VALUE) {
11                     dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
12                 }
13             }
14         }
15         return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
16     }
时间: 2024-10-15 09:34:51

322. Coin Change的相关文章

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

[email&#160;protected] [322] Coin Change (Dynamic Programming)

https://leetcode.com/problems/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

322. Coin Change java solutions

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,

322. Coin Change(leetcode )

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,

(Java) LeetCode 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,

Leetcode solution 322: Coin Change

Problem Statement  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 combin

【Leetcode】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,

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