322. Coin Change

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.

一刷
题解:
方法1: 用dynamic programming, int[] dp保存0-amout的结果。
但是这样会造成array out of boundary, 原因是空间复杂度可以达到O(amount)

public static 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){
                    if(dp[i]!=Integer.MAX_VALUE){
                        dp[i+coin] = Math.min(dp[i+coin], dp[i]+1);
                    }
                }
            }
        }
     
        if(dp[amount] >= Integer.MAX_VALUE)
            return -1;
     
        return dp[amount];
    }

方法2: Breath First Search (BFS)
amountQueue存储当前的coin sum up的值,step存储对应的硬币数目。
但是会出现超时的问题

public static int coinChange(int[] coins, int amount) {
        if(amount == 0) return 0;
        
        LinkedList<Integer> amountQueue = new LinkedList<Integer>();
        LinkedList<Integer> stepQueue = new LinkedList<Integer>();
        
        amountQueue.offer(0);//attach to the tail
        stepQueue.offer(0);
        
        while(amountQueue.size()>0){
            int temp = amountQueue.poll();
            int step = stepQueue.poll();
            
            if(temp == amount) return step;
            
            for(int coin : coins){
                if(temp<=amount){
                    if(!amountQueue.contains(temp+coin)){
                        amountQueue.offer(temp+coin);
                        stepQueue.offer(step+1);
                    }
                }
            }
        }
        
        return -1;
    }

二刷
用map替代dp数组做dp

class Solution {
    Map<Integer, Integer> amountDict = new HashMap<>();
    public int coinChange(int[] coins, int amount) {
        if(amount == 0) return 0;
        if(amountDict.containsKey(amount)) return amountDict.get(amount);
        int n = amount + 1;
        for(int coin : coins){
            int curr = 0;
            if(amount>=coin){
                int next = coinChange(coins, amount-coin);
                if(next>=0) curr = next+1;
            }
            if(curr>0) n = Math.min(n, curr);
        }
        
        int finalCount = (n==amount+1)? -1:n;
        amountDict.put(amount, finalCount);
        return finalCount;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Question You are given coins of different denominations a...
    FlynnLWang阅读 199评论 0 0
  • You are given coins of different denominations and a tota...
    Shiyi001阅读 280评论 0 0
  • 问题描述 You are given coins of different denominations and a...
    codingXue阅读 346评论 0 0
  • You are given coins of different denominations and a tota...
    exialym阅读 186评论 0 0
  • 急不来的是时间,留不住的还是时间,一直在路上,为了梦想,不停的前进! 一直不知道为什么要坚持,不知道为什么要那么努...
    照亮Br阅读 181评论 3 4