Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

DSA›Dynamic Programming›Coin Change
MediumDynamic Programming

Coin Change

dynamic-programmingdpbfs

Problem

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Examples

Example 1

Input: coins = [1,5,10,25], amount = 30

Output: 2

Explanation: 25 + 5 = 30

Example 2

Input: coins = [2], amount = 3

Output: -1

Explanation: Cannot make 3 from coin 2.

Constraints

  • •1 <= coins.length <= 12
  • •1 <= coins[i] <= 2^31 - 1
  • •0 <= amount <= 10^4

Hints

Hint 1

Build dp[0..amount] where dp[i] = min coins to make amount i. dp[i] = min(dp[i - coin] + 1) for each coin.

Solutions

public int coinChange(int[] coins, int amount) {
    int[] dp = new int[amount + 1];
    Arrays.fill(dp, amount + 1);  // infinity
    dp[0] = 0;
    for (int i = 1; i <= amount; i++) {
        for (int coin : coins) {
            if (coin <= i) {
                dp[i] = Math.min(dp[i], dp[i - coin] + 1);
            }
        }
    }
    return dp[amount] > amount ? -1 : dp[amount];
}
Java

Time: O(amount * coins.length) · Space: O(amount)