美文网首页
322. Coin Change

322. Coin Change

作者: 沉睡至夏 | 来源:发表于2016-12-23 04:39 被阅读5次

有点类似 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];
    }
}

相关文章

网友评论

      本文标题:322. Coin Change

      本文链接:https://www.haomeiwen.com/subject/vozhvttx.html