LeetCode – Coin Change (Java)

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

Java Solution 1 - Dynamic Programming

Let dp[v] to be the minimum number of coins required to get the amount v. 
dp[i+a_coin] = min(dp[i+a_coin], dp[i]+1) if dp[i] is reachable. 
dp[i+a_coin] = dp[i+a_coin] is dp[i] is not reachable. 
We initially set dp[i] to be MAX_VALUE. 

Here is the Java code:

public int coinChange(int[] coins, int amount) {
    if(amount==0) return 0;
    int[] dp = new int [amount+1];
    dp[0]=0; // do not need any coin to get 0 amount
    for(int i=1;i<=amount; i++)
        dp[i]= Integer.MAX_VALUE;
    for(int i=0; i<=amount; i++){
        for(int coin: coins){
            if(i+coin <=amount){
                    dp[i+coin] = dp[i+coin];
                    dp[i+coin] = Math.min(dp[i+coin], dp[i]+1);
    if(dp[amount] >= Integer.MAX_VALUE)
        return -1;
    return dp[amount];

This clean solution takes time O(n^2).

Java Solution 2 - Breath First Search (BFS)

Most dynamic programming problems can be solved by using BFS.

We can view this problem as going to a target position with steps that are allows in the array coins. We maintain two queues: one of the amount so far and the other for the minimal steps. The time is too much because of the contains method take n and total time is O(n^3).

public int coinChange(int[] coins, int amount) {
	if (amount == 0)
		return 0;
	LinkedList<Integer> amountQueue = new LinkedList<Integer>();
	LinkedList<Integer> stepQueue = new LinkedList<Integer>();
	// to get 0, 0 step is required
	while (amountQueue.size() > 0) {
		int temp = amountQueue.poll();
		int step = stepQueue.poll();
		if (temp == amount)
			return step;
		for (int coin : coins) {
			if (temp > amount) {
			} else {
				if (!amountQueue.contains(temp + coin)) {
					amountQueue.offer(temp + coin);
					stepQueue.offer(step + 1);
	return -1;
Category >> Algorithms >> Interview  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. anom on 2017-2-26

    The first solution should take O(k*n), where k is the amount and n is the number of denominations (2 nested loops)

  2. CSmith on 2017-3-16

    “Most dynamic programming problems can be solved by using BFS.” [citation needed]

  3. HM on 2017-4-12

    A cleaner loop:

    int [] f = new int[amount+1];
    for (int i=1; i<=amount; i++) {
    f[i] = Integer.MAX_VALUE;

    for (int i=1; i= coin) {
    if (f[i-coin] != Integer.MAX_VALUE) {
    f[i] = Math.min(f[i], 1 + f[i-coin]);

    if (f[amount] == Integer.MAX_VALUE) {
    return -1;
    return f[amount];

Leave a comment