java #Iterative Method:#。java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java #Iterative Method:#。java相关的知识,希望对你有一定的参考价值。

/*
[1,5,10,20,25] 40
Greedy Approach: 40 = 25 + 10 + 5
Expected Answer: 40 = 20 + 20
*/

public class Solution {
    int total = Integer.MAX_VALUE;
    
    public int coinChange(int[] coins, int amount) {
        if (amount == 0) {
            return 0;
        }
		Arrays.sort(coins);
		
		count(amount, coins.length - 1, coins, 0);
		return total == Integer.MAX_VALUE ? -1 : total;
    }
    
	private void count(int amount, int index, int[] coins, int count) {
		if (index < 0 ) {
		    return;
		}
		
		int c = amount / coins[index];
	    for (int i = c; i >= 0; i--) {
			int newCount = count + i;
			int rem = amount - i * coins[index];
			
			if (rem == 0) {
			    total = Math.min(total, newCount);
			} else {
			    if (newCount >= total - 1) {
			        break;
			    } else {
			        count(rem, index - 1, coins, newCount);
			    }
			}
		}
	}
}
/*
[1,5,10,20,25] 40
Greedy Approach: 40 = 25 + 10 + 5
Expected Answer: 40 = 20 + 20
*/

public class Solution {
    public int coinChange(int[] coins, int amount) {
        if (amount < 1) return 0;
        int[] dp = new int[amount + 1];
        int len = coins.length;
        Arrays.fill(dp, amount + 1);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < len; j++) {
                if (coins[j] <= i) {
                    dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                }
            }
        }
        return dp[amount] > amount ? -1 : dp[amount];
        
    }
    

}

public class Solution {
public int coinChange(int[] coins, int amount) {
    if(amount<1) return 0;
    return helper(coins, amount, new int[amount]);
}

private int helper(int[] coins, int rem, int[] count) { // rem: remaining coins after the last step; count[rem]: minimum number of coins to sum up to rem
    if(rem<0) return -1; // not valid
    if(rem==0) return 0; // completed
    if(count[rem-1] != 0) return count[rem-1]; // already computed, so reuse
    int min = Integer.MAX_VALUE;
    for(int coin : coins) {
        int res = helper(coins, rem-coin, count);
        if(res>=0 && res < min)
            min = 1+res;
    }
    count[rem-1] = (min==Integer.MAX_VALUE) ? -1 : min;
    return count[rem-1];
}
}
public class Solution {
public int coinChange(int[] coins, int amount) {
    if(amount<1) return 0;
    int[] dp = new int[amount+1];
    int sum = 0;
    
	while(++sum<=amount) {
		int min = -1;
    	for(int coin : coins) {
    		if(sum >= coin && dp[sum-coin]!=-1) {
    			int temp = dp[sum-coin]+1;
    			min = min<0 ? temp : (temp < min ? temp : min);
    		}
    	}
    	dp[sum] = min;
	}
	return dp[amount];
}
}

以上是关于java #Iterative Method:#。java的主要内容,如果未能解决你的问题,请参考以下文章

java #Iterative Method:#。java

java #Iterative Method:#。java

java #Iterative Method:#。java

java #Iterative Method:#。java

java #Iterative Method:#。java

java #Iterative Method:#。java