ปานกลาง 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 ได้ เข้าสู่ระบบและแก้โจทย์เพื่อร่วมพูดคุย