Linux   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[LeetCode 322] Coin Change大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

You are given coins of different denominations and a @R_736_10586@l 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

You are given coins of different denominations and a @R_736_10586@l 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.

 

BACkPack VI and Combination Sum IV:  Find all possible combinations that sum to a target value 

Coin Change: Find the combination that sums to a target value and uses the feWest number of elements

 

State: dp[i]: the feWest number of coins needed that sum to i

Function: dp[i] = Math.min(dp[i],dp[i - coins[j]] + 1),if i >= coins[j] && dp[i - coins[j]] < Integer.max_value

i >= coins[j]: only consider picking a coin if its value is not greater than the target value i;@H_801_61@

dp[i - coins[j]] < Integer.max_value: if we did pick coins[j],then we must be able to find a combination that sums @H_801_61@

to i - coins[j];@H_801_61@

Initialization: dp[0] = 0,dp[i] = Integer.max_value,for i >= 1

Answer: dp[amount] or -1

 

 1@H_801_61@ public@H_801_61@ class@H_801_61@ Solution {
@H_801_61@ 2@H_801_61@     public@H_801_61@ int@H_801_61@ coinChange(int@H_801_61@[] coins,int@H_801_61@ amount) {
@H_801_61@ 3@H_801_61@         if@H_801_61@(amount <= 0){
@H_801_61@ 4@H_801_61@             return@H_801_61@ 0;
@H_801_61@ 5@H_801_61@         }
@H_801_61@ 6@H_801_61@         if@H_801_61@(coins == null@H_801_61@ || coins.length == 0){
@H_801_61@ 7@H_801_61@             return@H_801_61@ -1;
@H_801_61@ 8@H_801_61@         }
@H_801_61@ 9@H_801_61@         int@H_801_61@[] dp = new@H_801_61@ int@H_801_61@[amount + 1];
@H_801_61@10@H_801_61@         dp[0] = 0;
@H_801_61@11@H_801_61@         for@H_801_61@(int@H_801_61@ i = 1; i <= amount; i++){
@H_801_61@12@H_801_61@             dp[i] = Integer.max_value;
@H_801_61@13@H_801_61@         }
@H_801_61@14@H_801_61@         for@H_801_61@(int@H_801_61@ i = 1; i <= amount; i++){
@H_801_61@15@H_801_61@             for@H_801_61@(int@H_801_61@ j = 0; j < coins.length; j++){
@H_801_61@16@H_801_61@                 if@H_801_61@(i >= coins[j] && dp[i - coins[j]] < Integer.max_value){
@H_801_61@17@H_801_61@                     dp[i] = Math.min(dp[i],dp[i - coins[j]] + 1);
@H_801_61@18@H_801_61@                 }
@H_801_61@19@H_801_61@             }
@H_801_61@20@H_801_61@         }
@H_801_61@21@H_801_61@         if@H_801_61@(dp[amount] < Integer.max_value){
@H_801_61@22@H_801_61@             return@H_801_61@ dp[amount];
@H_801_61@23@H_801_61@         }
@H_801_61@24@H_801_61@         return@H_801_61@ -1;
@H_801_61@25@H_801_61@     }
@H_801_61@26@H_801_61@ }

 

 

Related Problems

BACkPack VI

Combination Sum IV

大佬总结

以上是大佬教程为你收集整理的[LeetCode 322] Coin Change全部内容,希望文章能够帮你解决[LeetCode 322] Coin Change所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。