[LeetCode]Coin Change

经典动态规划

public class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] record = new int[amount + 1];
        record[0] = 1;
        for (int i = 1; i <= amount; i++) {
            record[i] = Integer.MAX_VALUE / 2;
            for (int j = 0; j < coins.length; j++) {
                int index = i - coins[j];
                if (index >= 0 && record[index] != 0) {
                    record[i] = Math.min(record[i], record[index] + 1);
                }
            }
        }
        return record[amount] == Integer.MAX_VALUE / 2 ? -1 : record[amount] - 1;
    }
}

 

posted @ 2015-12-29 14:21  Weizheng_Love_Coding  阅读(173)  评论(0编辑  收藏  举报