競賽程式設計簡單

最少金幣數量

“最小硬幣數量”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 min_coins(coins, V),傳回使用給定硬幣面額 coins 進行目標變更 V 所需的最小硬幣數。如果無法更改,則返回-1。

約束條件
  • 1 <= len(coins) <= 100
  • 1 <= V <= 10000
  • 1 <= coins[i] <= 1000

範例

Example 1
Input
min_coins([9, 6, 5, 1], 11)
Output
2
Explanation

We can make 11 using coins 6 and 5 (2 coins).

Example 2
Input
min_coins([5, 10], 7)
Output
-1
Explanation

We cannot make change for 7 using only coins of value 5 and 10.

Need a Hint?
考慮使用動態程式特定的資料結構,例如集合或堆。
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 資源

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