1. Top-Down DFS with Memoization
Recursively evaluate solve(rem) = 1 + min(solve(rem - coin)) across all denominations while caching previously solved remainders.
At most S distinct remainder states (1..amount) are evaluated, each branching over n coins.
Memo cache holds up to S entries plus up to S recursion depth.
How It Works
Caching Optimal Substructure Top-Down
For any remaining amount rem:
- If
rem == 0,0additional coins are needed. - If
rem < 0, that branch is invalid (return sentinelINT_MAX / infinity). - Otherwise try every
coin <= remand cache the minimum1 + solve(rem - coin)insidememo[rem].
This solves only reachable remainders cleanly via recursion.
Step-by-Step Dry Run
Try coins 1, 2, 5 -> branches down solve(10), solve(9), solve(6).
solve(11) -> branching- +Skips unreachable remainder states if denominations are large
- +Reads directly like the recursive math formula
- -Recursion stack overhead up to O(amount) calls (~10^4 depth)
Reference Implementation
Complete implementation for Top-Down DFS with Memoization in Python, JavaScript, Java, C++, and C.
def coin_change_memo(coins: list[int], amount: int) -> int:INF = amount + 1memo = {}def dfs(rem: int) -> int:if rem == 0:return 0if rem < 0:return INFif rem in memo:return memo[rem]best = INFfor c in coins:best = min(best, 1 + dfs(rem - c))memo[rem] = bestreturn bestans = dfs(amount)return ans if ans <= amount else -1
function coinChangeMemo(coins, amount) {
const INF = amount + 1;
const memo = new Int32Array(amount + 1).fill(-1);
function dfs(rem) {
if (rem === 0) return 0;
if (rem < 0) return INF;
if (memo[rem] !== -1) return memo[rem];
let best = INF;
for (const c of coins) {
if (c <= rem) {
best = Math.min(best, 1 + dfs(rem - c));
}
}
memo[rem] = best;
return best;
}
const res = dfs(amount);
return res > amount ? -1 : res;
}import java.util.Arrays;
public class Solution {
private static int dfs(int[] coins, int rem, int[] memo, int inf) {
if (rem == 0) return 0;
if (memo[rem] != -1) return memo[rem];
int best = inf;
for (int c : coins) {
if (c <= rem) {
best = Math.min(best, 1 + dfs(coins, rem - c, memo, inf));
}
}
return memo[rem] = best;
}
public static int coinChangeMemo(int[] coins, int amount) {
int inf = amount + 1;
int[] memo = new int[amount + 1];
Arrays.fill(memo, -1);
int ans = dfs(coins, amount, memo, inf);
return ans > amount ? -1 : ans;
}
}#include <vector>
#include <algorithm>
int dfsCoin(const std::vector<int>& coins, int rem, std::vector<int>& memo, int inf) {
if (rem == 0) return 0;
if (memo[rem] != -1) return memo[rem];
int best = inf;
for (int c : coins) {
if (c <= rem) {
best = std::min(best, 1 + dfsCoin(coins, rem - c, memo, inf));
}
}
return memo[rem] = best;
}
int coinChangeMemo(const std::vector<int>& coins, int amount) {
int inf = amount + 1;
std::vector<int> memo(amount + 1, -1);
int ans = dfsCoin(coins, amount, memo, inf);
return ans > amount ? -1 : ans;
}#include <stdlib.h>
static int dfsCoin(const int* coins, int n, int rem, int* memo, int inf) {
if (rem == 0) return 0;
if (memo[rem] != -1) return memo[rem];
int best = inf;
for (int i = 0; i < n; i++) {
if (coins[i] <= rem) {
int cand = 1 + dfsCoin(coins, n, rem - coins[i], memo, inf);
if (cand < best) best = cand;
}
}
return memo[rem] = best;
}
int coinChangeMemo(const int* coins, int n, int amount) {
int inf = amount + 1;
int* memo = (int*)malloc((amount + 1) * sizeof(int));
for (int i = 0; i <= amount; i++) memo[i] = -1;
int ans = dfsCoin(coins, n, amount, memo, inf);
free(memo);
return ans > amount ? -1 : ans;
}