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];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 自从大一大二参加完ACM之后又退出,很久没有碰算法了,工作也一年多了,重拾一下算法吧。 Coin Change 这...
    may丨be丶阅读 1,454评论 0 0
  • You are given coins of different denominations and a tota...
    HalcyonMoon阅读 134评论 0 0
  • You are given coins of different denominations and a tota...
    Jeanz阅读 468评论 0 0
  • You are given coins of different denominations and a tota...
    Shiyi001阅读 280评论 0 0
  • 事件的引发,从这周工作效率奇低开始说起。 为什么工作八年的专业培训人员,对最基本的培训事物性工作每周完成度不到4...
    appleye阅读 553评论 0 0