码迷,mamicode.com
首页 > 其他好文 > 详细

322. Coin Change

时间:2016-06-06 09:07:06      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

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.

Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)

Example 2:
coins = [2], amount = 3
return -1.

Note:
You may assume that you have an infinite number of each kind of coin.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

 

Subscribe to see which companies asked this question

Hide Tags
 Dynamic Programming
 

DP: Constructing the solution in the order of sum. ( Get 1, 2, ..., amount)

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) {
      //constructing the result from 1,2..., amount
      int min = -1;  // -1 means cannot get this amount.
      for (int coin : coins) {
        if (sum >= coin && dp[sum - coin] != -1) {
          int tempSolution = dp[sum - coin] + 1;
          if (min == -1)
            min = tempSolution;  //initialize solution.
          else
            min = Math.min(tempSolution, min);
        }
      }
      dp[sum] = min;
    }
    return dp[amount];
  }
}

 

 

DP: Constructing the solution by how many coins used. ( Use 1 coin, use 2 coins, ... until the target is reached.)

public class Solution {
    public int coinChange(int[] coins, int amount) {
        if(amount == 0)
            return 0;
        
        int len = amount+1;
        int[] result = new int[len];
        result[0] = 0;
        
        List<Integer> q = new ArrayList<Integer>();
        int minimumCoins = 1;
        for(Integer c : coins)
        {
            if(c < len)
            {
                result[c] = minimumCoins;
                q.add(c);
            }
        }
        
        while(!q.isEmpty())
        {
            ++minimumCoins;
            if(result[amount] != 0)
                return result[amount];
            List<Integer> next = new ArrayList<Integer>();
            
            for(Integer c : coins)
            {
                for(Integer optimal: q)
                {
                    int sum = c+optimal;
                    if(sum < len && result[sum] == 0)
                    {
                        //Find the new numbers that you can get with minimumCoins coins.
                        //Add one more coin to these numbers,
                        //you can get the next group of those optimial numbers.
                        result[sum] = minimumCoins;
                        next.add(sum);
                    }
                }
            }
            
            q = next;
        }
        
        return -1;
    }
}

 

322. Coin Change

标签:

原文地址:http://www.cnblogs.com/neweracoding/p/5562643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!