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的結(jié)果。
但是這樣會造成array out of boundary, 原因是空間復(fù)雜度可以達(dá)到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存儲當(dāng)前的coin sum up的值,step存儲對應(yīng)的硬幣數(shù)目。
但是會出現(xiàn)超時的問題

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數(shù)組做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;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 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
  • 急不來的是時間,留不住的還是時間,一直在路上,為了夢想,不停的前進(jìn)! 一直不知道為什么要堅持,不知道為什么要那么努...
    照亮Br閱讀 182評論 3 4