2Brute2Best
🔥0

Coin Change II

Medium·LeetCode #518 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given coins of several different values and a total amount. You have an unlimited supply of every coin.

Count how many different combinations of coins add up to exactly amount. Two combinations are different when they use a different number of some coin.

The order of the coins does not matter: 2 + 1 and 1 + 2 are the same combination. If the amount cannot be made, the answer is 0.

Input

amount, the total to make, and coins, a list of different coin values.

Output

The number of combinations of coins that add up to amount.

Example 1
Input: amount = 5, coins = [1, 2, 5]
Output: 4

The four combinations are 5; 2 + 2 + 1; 2 + 1 + 1 + 1; and 1 + 1 + 1 + 1 + 1.

Example 2
Input: amount = 3, coins = [2]
Output: 0

Only 2s are available, so 3 cannot be made.

Example 3
Input: amount = 10, coins = [10]
Output: 1

A single coin of 10 is the only combination.

Constraints
  • • 1 ≤ coins.length ≤ 300
  • • 1 ≤ coins[i] ≤ 5000
  • • All the values of coins are unique.
  • • 0 ≤ amount ≤ 5000
  • • The answer is guaranteed to fit in a signed 32-bit integer.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“number of combinations”
→ The goal is a count, not a min or max. Each ways[a] is a sum of counts for smaller amounts.
“unlimited supply of every coin”
→ Coins can repeat, so the amount loop goes low → high and reads ways[a − coin] from the same row.
“the order of the coins does not matter”
→ Coin types are the OUTER loop, so each combination is built in one fixed order.
03

Approach Complexity Ladder

Brute Force — skip or use each coin, in list orderO(2^(C + A))O(C + A)

Recurse on (coin index, amount left): skip the coin, or use it once more and stay on it

Better — 2D table: coin types × amountO(C · A)O(C · A)

ways[i][a] = combinations using the first i coin types; each cell is worked out once

Optimal — one row: coin types outside, amount low → high insideO(C · A)O(A)

One array ways[a]; for each coin type, go over the amounts low → high and add ways[a − coin]

Brute Force Intuition

Go through the coin types in order. For each one, either skip it and move on, or use one more copy and stay on the same coin, so it can repeat. Each path that reaches 0 is one combination. Coins are only taken in list order, so every combination is found once.

Recognition rule for this sub-pattern:

Items can repeat — capacity loop goes low → high. Order of loops decides combinations vs permutations.
Remember this · DP — Knapsack
Trigger
Pick a subset to hit a target sum / capacity, partition into two groups, count the ways to reach an amount.
Think
Is this "choose some items so their total is X"?
Key question
Can each item be used once (loop capacity backwards) or many times (loop forwards)?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Knapsack translate into production architectures and low-level memory mechanics.