Easy to understand JAVA solution


  • 0
    I
    public static int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount+1];
        for (int i = 0; i<dp.length;i++){
        	dp[i] = -1;
        }
        dp[0] = 0;
        for (int i = 0;i<dp.length;i++){
        	int min = Integer.MAX_VALUE;
            for (int j = 0; j<coins.length;j++){
                if ((i-coins[j])>=0 && dp[i-coins[j]]!=-1){
                	int temp = dp[i-coins[j]] +1;
                    if (temp<=min){
                        min = temp;
                        dp[i] = min;
                    }
                }
                
            }
            
        }
        return dp[dp.length-1];
    }

Log in to reply
 

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