322. Coin Change

有點類似 perfect squares那道題-279.

public class Solution {
    public int coinChange(int[] coins, int amount) {
        int dp[] = new int[amount+1];
        for (int i=1; i<=amount; i++) {
            int min_tmp = Integer.MAX_VALUE;
            for (int j=0; j<coins.length; j++) {
                // if the amount can be found in the coins array;
                if(i == coins[j]) {
                    min_tmp = 1;
                    break;
                // if cannot be found, but can keep on minus the coins value;
                } else if (i > coins[j] && dp[i-coins[j]] > 0 && dp[i-coins[j]]+1 < min_tmp)
                    min_tmp = dp[i-coins[j]]+1;
            }
            dp[i] = min_tmp == Integer.MAX_VALUE ? -1 : min_tmp;
        }
        return dp[amount];
    }
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 自從大一大二參加完ACM之后又退出,很久沒有碰算法了,工作也一年多了,重拾一下算法吧。 Coin Change 這...
    may丨be丶閱讀 1,495評論 0 0
  • You are given coins of different denominations and a tota...
    HalcyonMoon閱讀 172評論 0 0
  • You are given coins of different denominations and a tota...
    Jeanz閱讀 497評論 0 0
  • You are given coins of different denominations and a tota...
    Shiyi001閱讀 298評論 0 0
  • 事件的引發(fā),從這周工作效率奇低開始說起。 為什么工作八年的專業(yè)培訓人員,對最基本的培訓事物性工作每周完成度不到4...
    appleye閱讀 613評論 0 0

友情鏈接更多精彩內(nèi)容