Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11 Output: 3 Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3 Output: -1
Example 3:
Input: coins = [1], amount = 0 Output: 0
Constraints:
1 <= coins.length <= 121 <= coins[i] <= 231 - 10 <= amount <= 104Problem summary: You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. You may assume that you have an infinite number of each kind of coin.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,5] 11
[2] 3
[1] 0
minimum-cost-for-tickets)maximum-value-of-k-coins-from-piles)minimum-number-of-operations-to-convert-time)minimum-cost-to-split-an-array)count-of-sub-multisets-with-bounded-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #322: Coin Change
class Solution {
public int coinChange(int[] coins, int amount) {
final int inf = 1 << 30;
int m = coins.length;
int n = amount;
int[][] f = new int[m + 1][n + 1];
for (var g : f) {
Arrays.fill(g, inf);
}
f[0][0] = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= coins[i - 1]) {
f[i][j] = Math.min(f[i][j], f[i][j - coins[i - 1]] + 1);
}
}
}
return f[m][n] >= inf ? -1 : f[m][n];
}
}
// Accepted solution for LeetCode #322: Coin Change
func coinChange(coins []int, amount int) int {
m, n := len(coins), amount
f := make([][]int, m+1)
const inf = 1 << 30
for i := range f {
f[i] = make([]int, n+1)
for j := range f[i] {
f[i][j] = inf
}
}
f[0][0] = 0
for i := 1; i <= m; i++ {
for j := 0; j <= n; j++ {
f[i][j] = f[i-1][j]
if j >= coins[i-1] {
f[i][j] = min(f[i][j], f[i][j-coins[i-1]]+1)
}
}
}
if f[m][n] > n {
return -1
}
return f[m][n]
}
# Accepted solution for LeetCode #322: Coin Change
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
m, n = len(coins), amount
f = [[inf] * (n + 1) for _ in range(m + 1)]
f[0][0] = 0
for i, x in enumerate(coins, 1):
for j in range(n + 1):
f[i][j] = f[i - 1][j]
if j >= x:
f[i][j] = min(f[i][j], f[i][j - x] + 1)
return -1 if f[m][n] >= inf else f[m][n]
// Accepted solution for LeetCode #322: Coin Change
impl Solution {
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
let n = amount as usize;
let mut f = vec![n + 1; n + 1];
f[0] = 0;
for &x in &coins {
for j in x as usize..=n {
f[j] = f[j].min(f[j - (x as usize)] + 1);
}
}
if f[n] > n {
-1
} else {
f[n] as i32
}
}
}
// Accepted solution for LeetCode #322: Coin Change
function coinChange(coins: number[], amount: number): number {
const m = coins.length;
const n = amount;
const f: number[][] = Array(m + 1)
.fill(0)
.map(() => Array(n + 1).fill(1 << 30));
f[0][0] = 0;
for (let i = 1; i <= m; ++i) {
for (let j = 0; j <= n; ++j) {
f[i][j] = f[i - 1][j];
if (j >= coins[i - 1]) {
f[i][j] = Math.min(f[i][j], f[i][j - coins[i - 1]] + 1);
}
}
}
return f[m][n] > n ? -1 : f[m][n];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.