Skip to main content

Coin Change II

Given an array of coin denominations and a target amount Return the number of different combinations that make up the amount You have infinite coins of each denomination Example: amount = 5, coins = [1,2,5] => 4 The combinations are: [5], [2,2,1], [2,1,1,1], [1,1,1,1,1]

Given a non-negative integer amount and an array coins of distinct positive integer denominations, return the number of combinations of coins whose values sum to amount. Each denomination may be used any number of times, and choosing the same coins in a different order does not create a new combination. There is one combination for amount 0, using no coins, and the result is 0 when the amount cannot be formed. The answer fits in a signed 32-bit integer.

Examples

You write

change(amount: int, coins: List[int])

  1. Example 1

    Input
    amount = 5, coins = [1,2,5]
    returns
    4
  2. Example 2

    Input
    amount = 3, coins = [2]
    returns
    0

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Dynamic Programming

I reach for it when

  • Brute force repeats the same subproblem.
  • You are asked for a count, a minimum or a maximum.

Say this before you type. Say the state in one sentence, then say which smaller states it reads from.

All 37 Dynamic Programming problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Number of Ways to Make Amountmedium

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.