Given a list of coin denominations and a target amount, find the minimum number of coins needed to make up that exact amount. Each coin denomination can be used an unlimited number of times. Return -1 if it is not possible to make up the amount using the given coins. Return 0 if amount is 0. Return -1 for null or invalid inputs.

Examples:

Input: coins = [1, 5, 10, 25], amount = 36 Output: 3 Explanation: Use 25 + 10 + 1 = 36, which requires 3 coins Input: coins = [1, 5, 10], amount = 15 Output: 2 Explanation: Use 10 + 5 = 15, which requires 2 coins Input: coins = [2], amount = 3 Output: -1 Explanation: Cannot make 3 using only coins of denomination 2 Input: coins = [1, 2, 5], amount = 0 Output: 0 Explanation: Zero amount requires zero coins Input: coins = null, amount = 10 Output: -1
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay September 2026 (Sep 16–30)Sponsored by FLXBL

Contest runs September 16 - 30. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In