0
import java.util.*;


public class CoinChange {
    public static int coinChange(int[] coins, int amount) {
        // Create an array to store the minimum number of coins needed to make change for each amount
        int[] dp = new int[amount + 1];
        ArrayList<Integer> used = new ArrayList<Integer>();
        int highestval = 0;
        
        // Initialize the array with a value greater than the maximum amount, except for dp[0]
        Arrays.fill(dp, amount + 1);
        dp[0] = 0;
        
        
        
        // Loop through all amounts from 1 to amount
        for (int i = 1; i <= amount; i++) {
            // Loop through all coins
            for (int j = 0; j < coins.length; j++) {
                // If the current coin can be used to make change for i
                if (coins[j] <= i) {
                    // Update dp[i] to be the minimum of its current value and dp[i - coins[j]] + 1
                    dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                    if(dp[i]>highestval) {
                        used.add(coins[j]);
                        highestval = dp[i];
                    }else if(dp[i]<highestval){
                        if(dp[i] == 1)
                        {
                            used.clear();
                            used.add(coins[j]);
                            highestval = dp[i];
                        }
                        else {
                            int toGoBack = highestval - dp[i];
                            while(toGoBack>0)
                            {
                                used.remove(used.size()-1);
                                highestval--;
                                toGoBack--;
                            }
                        }
                        
                    }
                    
                }
            }
        }
        
       
        // If dp[amount] is still greater than the maximum amount, there is no solution
        if (dp[amount] > amount) {
            return -1;
        } else {
            // Otherwise, return the minimum number of coins needed to make change for amount
            System.out.print("Coins Used: " + used.toString());
            
            System.out.println();
            return dp[amount];
        }
    }

    public static void main(String[] args) {
        
        int[] coins = {1,5,10,25};
        int amount = 34;
        System.out.println(coinChange(coins,amount));
        }
}

Current Output: Coins Used: [25, 1, 1, 1, 1, 1] 6

Currently Trying to keep track of the coins used with array lists and it will keep adding to the list until it detects that the number of coins used is reduced.If it goes to 1 coin used it will clear the list and add the new coin denomination or if its a value grater than 1 I will have a loop that pops off the end of the list for the distance between the current highest value(current length) until that value reaches 0.Expected output is clearly not right can anyone see where I went wrong?

Crimz
  • 3
  • 3

0 Answers0