150強訪談中等

硬幣找零 II

「硬幣找零 II」問題的詳細指南和 Python 實作。

問題陳述

中等

給你一個代表不同面額硬幣的整數數組硬幣和代表總金額的整數金額。

傳回組成該金額的組合數。如果任何硬幣組合都無法彌補該金額,則返回 0。

您可能會假設您擁有無限數量的每種硬幣。

寫一個函數 change(amount: int, coins: List[int]) -> int

約束條件
  • 1 <= len(coins) <= 300
  • 1 <= coins[i] <= 5000
  • 0 <= amount <= 5000

範例

Example 1
Input
amount = 5, coins = [1,2,5]
Output
4
Explanation

There are four ways to make up the amount: 5, 2+2+1, 2+1+1+1, 1+1+1+1+1.

Example 2
Input
amount = 3, coins = [2]
Output
0
Explanation

The amount of 3 cannot be made up with just 2s.

Need a Hint?
考慮使用 2D DP 特定的資料結構,例如集合或堆疊。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。