ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode Coin Change Series

2020-11-26 09:05:16  阅读:211  来源: 互联网

标签:return int Series coins length amount Coin LeetCode dp


classic dp problem
322
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.

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

this is a classic knapsack problem.
dp[amount+1][coins.length+1]
dp[i][j] means that we only have amount of i needs to get change, and we only have j kind of coins to change
i tried to write that, but i failed,

class Solution {
    public int coinChange(int[] coins, int amount) {
        int m = coins.length;
        int[][] dp = new int[coins.length+1][amount+1];
        dp[0][0] = 0;
        for (int i = 1; i <= m; i++) {
            dp[i][0] = 0; //when amount is 0
        }
        for (int i = 1; i<=amount; i++) {
            dp[0][i] = 0;//we coin is 0
        }
        for (int j = 1; j <= amount; j++) {
            for (int i = 1; i<= m; i++) { //because we can choose as many coins as we want. that's why we put coins in the inner loop
                if (j - coins[i-1] > 0) {
                    dp[i][j] = Math.min(dp[i-1][j], dp[i-1][j - coins[i-1]] + 1);
                } else {
                    dp[i][j] = dp[i-1][j];//didn't choose this becasue of not enought space
                }
            }
        }
        return dp[m][amount];
    }
}

and then i know what i did wrong

class Solution {
    public int coinChange(int[] coins, int amount) {
        int m = coins.length;
        int[][] dp = new int[coins.length+1][amount+1];
        dp[0][0] = 0;
        for (int i = 1; i <= m; i++) {
            dp[i][0] = 0; //when amount is 0
        }
        for (int i = 1; i<=amount; i++) {
            dp[0][i] = Integer.MAX_VALUE;//the amount of that
        }
        for (int j = 1; j <= amount; j++) {
            for (int i = 1; i<= m; i++) { //because we can choose as many coins as we want. that's why we put coins in the inner loop
                if (j - coins[i-1] > 0) {
                    dp[i][j] = Math.min(dp[i-1][j], dp[i-1][j - coins[i-1]] + 1);
                } else {
                    dp[i][j] = dp[i-1][j];//didn't choose this becasue of not enought space
                }
            }
        }
        return dp[m][amount]; 
    }
}

but it’s overflowed still.
because of the dp[i-1][j - coins[i-1]] + 1 part might makes it overflowed.
so we need to assign dp[0][i] = amount+1 //don’t use it too precise, you will just make yourself look bad.

class Solution {
    public int coinChange(int[] coins, int amount) {
        int m = coins.length;
        int[][] dp = new int[coins.length+1][amount+1];
        dp[0][0] = 0;
        for (int i = 1; i <= m; i++) {
            dp[i][0] = 0; //when amount is 0
        }
        for (int i = 1; i<=amount; i++) {
            dp[0][i] = amount + 1;//unreachable 
        }
        for (int j = 1; j <= amount; j++) {
            for (int i = 1; i<= m; i++) { //because we can choose as many coins as we want. that's why we put coins in the inner loop
                
                if (j - coins[i-1] >= 0) {
                    dp[i][j] = Math.min(dp[i-1][j], dp[i][j - coins[i-1]] + 1);
                } else {
                    dp[i][j] = dp[i-1][j];//didn't choose this becasue of not enought space
                }
            }
        }
        return dp[m][amount] > amount? -1: dp[m][amount]; 
    }
}

518
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
//but in this problem, order is not matter
//I found out that this problem is a more general version of k sum, k sum means you have to do it using k number. but this problem can use as many as you can.
//permuatation is easy than combination, because combination needs to eliminate all the duplicates results in permutation.
//we can see in the code, it is very simililar to LC377, except, now amount is the inner loop? why does the amount be the inner loop makes the solution didn’t count the duplicates.

we use 1D array to solve this:
dp[i] reepresents for the number of combinations if the amount the money we need to change is i

class Solution {
    public int change(int amount, int[] coins) {
        if(amount == 0) return 1;
        if(coins == null || coins.length == 0) return 0;
        
        int[] dp = new int[amount + 1];
        dp[0] = 1;
        
        for(int i = 0; i<coins.length; i++) {
            for (int j = 1; j<=amount; j++) {
                if(j-coins[i] >= 0) {
                    dp[j] += dp[j - coins[i]];  //because we can choose one single kind of coin unlimited time, that's why we need to make that outer loop
                }
            }
        }
        // for(int j = 1; j<=amount; j++) {
        //     for (int i = 0; i<coins.length; i++) {
        //         if(j-coins[i] >= 0) {
        //             dp[j] += dp[j - coins[i]]; 
        //         }
        //     }
        // }
        return dp[amount];
    }
}

now, we compare this code to LC377 combination sum IV

//now, the problem is: can we using 2-D array to solve this problem?
//and the answer is yes!
//the code is as follows: and the inner loop and outer loop can reversed.
// for(int i=1;i<coins.length;i++)
//   {
//     for(int j=1;j<=amount;j++)
//     {
//       if(j>=coins[i])
//         dp[i][j]=dp[i-1][j]+dp[i][j-coins[i]];
//       else
//          dp[i][j]=dp[i-1][j];
//     }
//   }
class Solution {
    public int combinationSum4(int[] nums, int target) {
        
        if(target == 0) return 1;
        if(nums == null || nums.length == 0) return 0;
        
        int[] dp = new int[target+1];
        for(int i = 1; i<=target; i++) {
            dp[i] = 0;
        }
        dp[0] = 1;
        
        //so the general idea of follow for loops, is: for each target, we will try all the coins, if we can use this coin, then fine. after we used this coin, nothing is change in nums[] becasue each one of them has unlimited number.
        for(int i = 1; i<=target; i++) {
            for(int j = 0; j<nums.length; j++) { //try every kind of coin
                if(i-nums[j] >= 0) { //if current coin value <= current target, means we can cover it. so now dp[i] is its origianl number of ways plus if we choose nums[j]
                    dp[i] += dp[i-nums[j]];
                }
            }
        }
        return dp[target];
    }
}

标签:return,int,Series,coins,length,amount,Coin,LeetCode,dp
来源: https://blog.csdn.net/weixin_44337445/article/details/110123757

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有