DP java solution


  • 0
    A
    public int coinChange(int[] coins, int amount) {
    
    	if (amount < 0 || coins == null || coins.length == 0) {
    
    		return -1;
    	}
    
    	if (amount == 0) {
    		return 0;
    	}
    
    	long a[] = new long[amount + 1];
    
    	for (int i = 0; i < a.length; i++) {
    		a[i] = Integer.MAX_VALUE;
    	}
    
    	a[0] = 0;
    	for (int i = 1; i <= amount; i++) {
    
    		for (int c = 0; c < coins.length; c++) {
    
    			if (i - coins[c] >= 0) {
    				a[i] = Math.min(a[i], a[i - coins[c]] + 1);
    			}
    		}
    	}
    	if (a[amount] == Integer.MAX_VALUE) {
    		return -1;
    	}
    
    	return (int )a[amount];
    
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.