Все задачи
Средне Решена: 1 Процент решений: 100.0%

Count Coin Combinations

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

Чему вы научитесь

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

Условие

Показать перевод

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.

Ограничения

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.

Примеры

Пример 1
Ввод:
amount = 4
coins = [1, 2]

Ожидаемый вывод:
expected = 3
Ввод:
long long amount = 4;
std::vector<long long> coins = {1, 2};

Ожидаемый вывод:
long long expected = 3;
Ввод:
long amount = 4;
long[] coins = new long[] {1, 2};

Ожидаемый вывод:
long expected = 3;

Пояснение

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

Пример 2
Ввод:
amount = 3
coins = [2]

Ожидаемый вывод:
expected = 0
Ввод:
long long amount = 3;
std::vector<long long> coins = {2};

Ожидаемый вывод:
long long expected = 0;
Ввод:
long amount = 3;
long[] coins = new long[] {2};

Ожидаемый вывод:
long expected = 0;

Пояснение

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

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

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

Подсказка 2

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

Подсказка 3

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

Ожидаемая сложность

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

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

# Участник Язык Время Память Оценка ИИ Решение
1 ННикита Python 811 ms - 5/5 Посмотреть код

Учитесь у других

Решения сообщества

Внимание, спойлер: ниже 1 полное решение Попробуйте сначала решить сами. Вы всегда можете посмотреть код других и AI-обзоры. Войдите и решите задание, чтобы присоединиться к обсуждению.