package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/coin-change-2/
 */
public class Problem518 {
    public int change(int amount, int[] coins) {
        return coinChange(amount, coins, new HashMap<>());
    }

    private static int coinChange(int amount, int[] coins, Map<Integer, Integer> memo) {
        if (amount <= 0) {
            if (amount == 0) {
                return 1;
            }
            return 0;
        }
        if (memo.containsKey(amount)) {
            return memo.get(amount);
        }
        int count = 0;
        for (int coin : coins) {
            count += coinChange(amount - coin, coins, memo);
        }
        memo.put(amount, count);
        return count;
    }

    public static void main(String[] args) {
        Problem518 prob = new Problem518();
        System.out.println(prob.change(5, new int[]{1, 2, 5})); // 4
//        System.out.println(prob.change(3, new int[]{2})); // 0
//        System.out.println(prob.change(10, new int[]{10})); // 1
    }
}
@@ -232,16 +232,19 @@
 , coins,
+ 0,
  new Has
@@ -319,16 +319,25 @@
 %5D coins,
+ int idx,
  Map%3CInt
@@ -620,20 +620,38 @@
 int 
-coin : coins
+i = idx; i %3C coins.length; i++
 ) %7B%0A
@@ -695,24 +695,28 @@
 t - coin
+s%5Bi%5D
 , coins,
  memo);%0A
@@ -707,16 +707,19 @@
 , coins,
+ i,
  memo);%0A

Update problem 518