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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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

🏆 All Time Leaderboard

Contest runs January 1, 1970 - December 31, 9999. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In