모든 챌린지
보통 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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 811 ms - 5/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 솔루션

스포일러: 아래에 1개 전체 솔루션 있음 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.