package leetcode;

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

/**
 * https://leetcode.com/problems/coin-change/
 */
public class Problem322 {
    public int coinChange(int[] coins, int amount) {
        Map<Integer, Integer> memo = new HashMap<>();
        int result = coinChange(coins, amount, memo);
        if (result == Integer.MAX_VALUE) {
            return -1;
        }
        return result - 1;
    }

    private int coinChange(int[] coins, int amount, Map<Integer, Integer> memo) {
        if (amount < 0) {
            return Integer.MAX_VALUE;
        } else if (amount == 0) {
            return 1;
        }
        if (memo.containsKey(amount)) {
            return memo.get(amount);
        }
        int result = Integer.MAX_VALUE;
        for (int coin : coins) {
            int tmp = coinChange(coins, amount - coin, memo);
            if (tmp != Integer.MAX_VALUE) {
                result = Math.min(result, tmp);
            }
        }
        if (result != Integer.MAX_VALUE) {
            result++;
        }
        memo.put(amount, result);
        return result;
    }
}

@@ -16,57 +16,8 @@
 e;%0A%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -153,37 +153,25 @@
         
-Map%3C
 Integer
-, Integer%3E
+%5B%5D
  memo = 
@@ -178,19 +178,27 @@
 new 
-HashMap%3C%3E()
+Integer%5Bamount + 1%5D
 ;%0A  
@@ -371,16 +371,23 @@
 private 
+static 
 int coin
@@ -422,29 +422,17 @@
 nt, 
-Map%3C
 Integer
-, Integer%3E
+%5B%5D
  mem
@@ -586,28 +586,24 @@
 memo
-.containsKey(amount)
+%5Bamount%5D != null
 ) %7B%0A
@@ -629,20 +629,16 @@
 memo
-.get(
+%5B
 amount
-)
+%5D
 ;%0A  
@@ -987,28 +987,25 @@
 memo
-.put(
+%5B
 amount
-,
+%5D =
  result
-)
 ;%0A  

Update problem 322