Voltar para os desafios
Médio 0 resolvidos 0% de aceitação

Count Coin Combinations

Count ways to make an amount with unlimited coins of specified denominations.

O que você vai aprender

  • Count combinations, not orderings
  • Loop over coins in the outer loop
  • Build a 1D DP array of ways

Problema

Ver tradução

Use any number of each supplied denomination to make amount exactly. Count combinations, not orderings: two arrangements using the same counts of each coin are the same way. Denominations are distinct and positive.

Implement coin_sums(amount, coins).

Input

amount: The total amount to make.

coins: The available distinct denominations.

Output

Return the number of unordered combinations.

Restrições

0 ≤ amount ≤ 200. There are 0 through 10 distinct denominations, each from 1 through 200. All counts fit signed 64-bit. Amount 0 has one way: choose no coins.

Exemplos

Exemplo 1
Entrada:
coins = [1, 2]
amount = 4

Saída esperada:
expected = 3
Entrada:
std::vector<long long> coins = {1, 2};
long long amount = 4;

Saída esperada:
long long expected = 3;
Entrada:
long[] coins = new long[] {1, 2};
long amount = 4;

Saída esperada:
long expected = 3;

Explicação

The combinations are four 1s, two 1s with one 2, and two 2s.

Exemplo 2
Entrada:
coins = [2]
amount = 3

Saída esperada:
expected = 0
Entrada:
std::vector<long long> coins = {2};
long long amount = 3;

Saída esperada:
long long expected = 0;
Entrada:
long[] coins = new long[] {2};
long amount = 3;

Saída esperada:
long expected = 0;

Explicação

Any sum of 2-valued coins is even, so 3 is impossible.

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

The order of coins does not matter, so combinations should be counted by denomination, not by permutation.

Dica 2

Build the number of ways for each amount incrementally, letting each coin contribute to all reachable totals.

Dica 3

Be careful with amount zero, which always has one valid way: choose no coins.

Complexidade esperada

O(amount × number_of_coins) time, O(amount) extra space

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.