Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Coin Change 2 problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Coin Change 2 problem solution You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.

You may assume that you have an infinite number of each kind of coin.

The answer is guaranteed to fit into a signed 32-bit integer.

Leetcode Coin Change 2 problem solution

Leetcode Coin Change 2 problem solution in Python.

class Solution(object):
    def change(self, amount, coins):
        
        lst=[1]+[0]*amount
        
        for coin in coins:
            for i in range(coin,amount+1):
                lst[i]+=lst[i-coin] 
                
        return lst[-1]

Coin Change 2 problem solution in Java.

public int change(int amount, int[] coins) {
        if(amount == 0){ // u can make amt = 0 in 1 way ie don't give any coins
            return 1; 
        }
        
        int [] dp = new int[amount+1]; 
        dp[0] = 1; 
        
        for(int coin : coins){ // u should loop coins over the amount to avoid permutations
            for(int i=1; i<dp.length; i++){
                if(i >= coin){
                    dp[i] += dp[i-coin]; 
                }
            }
        }
        
        return dp[amount]; 
    }

Problem solution in C++.

class Solution {
public:
    int change(int amount, vector<int>& coins) {
        vector<vector<int>> dp(coins.size() + 1, vector<int> (amount + 1));
        dp[0][0] = 1;
        for(int i = 1; i < coins.size() + 1; i++){
            dp[i][0] = 1;
            for(int j = 1; j < amount + 1; j++){
                dp[i][j] = dp[i - 1][j] + (j >= coins[i-1] ? dp[i][j - coins[i - 1]] : 0);
            }
        }
        return dp[coins.size()][amount];
    }
};

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes